001/* 002 * Copyright 2002-2016 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 019/** 020 * Super class for exceptions that can occur whilst processing expressions. 021 * 022 * @author Andy Clement 023 * @author Phil Webb 024 * @since 3.0 025 */ 026@SuppressWarnings("serial") 027public class ExpressionException extends RuntimeException { 028 029 protected String expressionString; 030 031 protected int position; // -1 if not known; should be known in all reasonable cases 032 033 034 /** 035 * Construct a new expression exception. 036 * @param message a descriptive message 037 */ 038 public ExpressionException(String message) { 039 super(message); 040 } 041 042 /** 043 * Construct a new expression exception. 044 * @param message a descriptive message 045 * @param cause the underlying cause of this exception 046 */ 047 public ExpressionException(String message, Throwable cause) { 048 super(message, cause); 049 } 050 051 /** 052 * Construct a new expression exception. 053 * @param expressionString the expression string 054 * @param message a descriptive message 055 */ 056 public ExpressionException(String expressionString, String message) { 057 super(message); 058 this.expressionString = expressionString; 059 this.position = -1; 060 } 061 062 /** 063 * Construct a new expression exception. 064 * @param expressionString the expression string 065 * @param position the position in the expression string where the problem occurred 066 * @param message a descriptive message 067 */ 068 public ExpressionException(String expressionString, int position, String message) { 069 super(message); 070 this.expressionString = expressionString; 071 this.position = position; 072 } 073 074 /** 075 * Construct a new expression exception. 076 * @param position the position in the expression string where the problem occurred 077 * @param message a descriptive message 078 */ 079 public ExpressionException(int position, String message) { 080 super(message); 081 this.position = position; 082 } 083 084 /** 085 * Construct a new expression exception. 086 * @param position the position in the expression string where the problem occurred 087 * @param message a descriptive message 088 * @param cause the underlying cause of this exception 089 */ 090 public ExpressionException(int position, String message, Throwable cause) { 091 super(message, cause); 092 this.position = position; 093 } 094 095 096 /** 097 * Return the expression string. 098 */ 099 public final String getExpressionString() { 100 return this.expressionString; 101 } 102 103 /** 104 * Return the position in the expression string where the problem occurred. 105 */ 106 public final int getPosition() { 107 return this.position; 108 } 109 110 /** 111 * Return the exception message. 112 * As of Spring 4.0, this method returns the same result as {@link #toDetailedString()}. 113 * @see #getSimpleMessage() 114 * @see java.lang.Throwable#getMessage() 115 */ 116 @Override 117 public String getMessage() { 118 return toDetailedString(); 119 } 120 121 /** 122 * Return a detailed description of this exception, including the expression 123 * String and position (if available) as well as the actual exception message. 124 */ 125 public String toDetailedString() { 126 if (this.expressionString != null) { 127 StringBuilder output = new StringBuilder(); 128 output.append("Expression ["); 129 output.append(this.expressionString); 130 output.append("]"); 131 if (this.position >= 0) { 132 output.append(" @"); 133 output.append(this.position); 134 } 135 output.append(": "); 136 output.append(getSimpleMessage()); 137 return output.toString(); 138 } 139 else { 140 return getSimpleMessage(); 141 } 142 } 143 144 /** 145 * Return the exception simple message without including the expression 146 * that caused the failure. 147 * @since 4.0 148 */ 149 public String getSimpleMessage() { 150 return super.getMessage(); 151 } 152 153}