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.spel;
018
019import org.springframework.expression.EvaluationException;
020
021/**
022 * Root exception for Spring EL related exceptions. Rather than holding a hard coded
023 * string indicating the problem, it records a message key and the inserts for the
024 * message. See {@link SpelMessage} for the list of all possible messages that can occur.
025 *
026 * @author Andy Clement
027 * @author Juergen Hoeller
028 * @since 3.0
029 */
030@SuppressWarnings("serial")
031public class SpelEvaluationException extends EvaluationException {
032
033        private final SpelMessage message;
034
035        private final Object[] inserts;
036
037
038        public SpelEvaluationException(SpelMessage message, Object... inserts) {
039                super(message.formatMessage(inserts));
040                this.message = message;
041                this.inserts = inserts;
042        }
043
044        public SpelEvaluationException(int position, SpelMessage message, Object... inserts) {
045                super(position, message.formatMessage(inserts));
046                this.message = message;
047                this.inserts = inserts;
048        }
049
050        public SpelEvaluationException(int position, Throwable cause, SpelMessage message, Object... inserts) {
051                super(position, message.formatMessage(inserts), cause);
052                this.message = message;
053                this.inserts = inserts;
054        }
055
056        public SpelEvaluationException(Throwable cause, SpelMessage message, Object... inserts) {
057                super(message.formatMessage(inserts), cause);
058                this.message = message;
059                this.inserts = inserts;
060        }
061
062
063        /**
064         * Set the position in the related expression which gave rise to this exception.
065         */
066        public void setPosition(int position) {
067                this.position = position;
068        }
069
070        /**
071         * Return the message code.
072         */
073        public SpelMessage getMessageCode() {
074                return this.message;
075        }
076
077        /**
078         * Return the message inserts.
079         */
080        public Object[] getInserts() {
081                return this.inserts;
082        }
083
084}