001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.expression; 018 019import org.springframework.lang.Nullable; 020 021/** 022 * Super class for exceptions that can occur whilst processing expressions. 023 * 024 * @author Andy Clement 025 * @author Phillip Webb 026 * @since 3.0 027 */ 028@SuppressWarnings("serial") 029public class ExpressionException extends RuntimeException { 030 031 @Nullable 032 protected final String expressionString; 033 034 protected int position; // -1 if not known; should be known in all reasonable cases 035 036 037 /** 038 * Construct a new expression exception. 039 * @param message a descriptive message 040 */ 041 public ExpressionException(String message) { 042 super(message); 043 this.expressionString = null; 044 this.position = 0; 045 } 046 047 /** 048 * Construct a new expression exception. 049 * @param message a descriptive message 050 * @param cause the underlying cause of this exception 051 */ 052 public ExpressionException(String message, Throwable cause) { 053 super(message, cause); 054 this.expressionString = null; 055 this.position = 0; 056 } 057 058 /** 059 * Construct a new expression exception. 060 * @param expressionString the expression string 061 * @param message a descriptive message 062 */ 063 public ExpressionException(@Nullable String expressionString, String message) { 064 super(message); 065 this.expressionString = expressionString; 066 this.position = -1; 067 } 068 069 /** 070 * Construct a new expression exception. 071 * @param expressionString the expression string 072 * @param position the position in the expression string where the problem occurred 073 * @param message a descriptive message 074 */ 075 public ExpressionException(@Nullable String expressionString, int position, String message) { 076 super(message); 077 this.expressionString = expressionString; 078 this.position = position; 079 } 080 081 /** 082 * Construct a new expression exception. 083 * @param position the position in the expression string where the problem occurred 084 * @param message a descriptive message 085 */ 086 public ExpressionException(int position, String message) { 087 super(message); 088 this.expressionString = null; 089 this.position = position; 090 } 091 092 /** 093 * Construct a new expression exception. 094 * @param position the position in the expression string where the problem occurred 095 * @param message a descriptive message 096 * @param cause the underlying cause of this exception 097 */ 098 public ExpressionException(int position, String message, Throwable cause) { 099 super(message, cause); 100 this.expressionString = null; 101 this.position = position; 102 } 103 104 105 /** 106 * Return the expression string. 107 */ 108 @Nullable 109 public final String getExpressionString() { 110 return this.expressionString; 111 } 112 113 /** 114 * Return the position in the expression string where the problem occurred. 115 */ 116 public final int getPosition() { 117 return this.position; 118 } 119 120 /** 121 * Return the exception message. 122 * As of Spring 4.0, this method returns the same result as {@link #toDetailedString()}. 123 * @see #getSimpleMessage() 124 * @see java.lang.Throwable#getMessage() 125 */ 126 @Override 127 public String getMessage() { 128 return toDetailedString(); 129 } 130 131 /** 132 * Return a detailed description of this exception, including the expression 133 * String and position (if available) as well as the actual exception message. 134 */ 135 public String toDetailedString() { 136 if (this.expressionString != null) { 137 StringBuilder output = new StringBuilder(); 138 output.append("Expression ["); 139 output.append(this.expressionString); 140 output.append("]"); 141 if (this.position >= 0) { 142 output.append(" @"); 143 output.append(this.position); 144 } 145 output.append(": "); 146 output.append(getSimpleMessage()); 147 return output.toString(); 148 } 149 else { 150 return getSimpleMessage(); 151 } 152 } 153 154 /** 155 * Return the exception simple message without including the expression 156 * that caused the failure. 157 * @since 4.0 158 */ 159 public String getSimpleMessage() { 160 return super.getMessage(); 161 } 162 163}