001/*
002 * Copyright 2002-2017 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.web.server;
018
019import org.springframework.core.MethodParameter;
020import org.springframework.http.HttpStatus;
021import org.springframework.lang.Nullable;
022
023/**
024 * Exception for errors that fit response status 400 (bad request) for use in
025 * Spring Web applications. The exception provides additional fields (e.g.
026 * an optional {@link MethodParameter} if related to the error).
027 *
028 * @author Rossen Stoyanchev
029 * @since 5.0
030 */
031@SuppressWarnings("serial")
032public class ServerWebInputException extends ResponseStatusException {
033
034        @Nullable
035        private final MethodParameter parameter;
036
037
038        /**
039         * Constructor with an explanation only.
040         */
041        public ServerWebInputException(String reason) {
042                this(reason, null, null);
043        }
044
045        /**
046         * Constructor for a 400 error linked to a specific {@code MethodParameter}.
047         */
048        public ServerWebInputException(String reason, @Nullable MethodParameter parameter) {
049                this(reason, parameter, null);
050        }
051
052        /**
053         * Constructor for a 400 error with a root cause.
054         */
055        public ServerWebInputException(String reason, @Nullable MethodParameter parameter, @Nullable Throwable cause) {
056                super(HttpStatus.BAD_REQUEST, reason, cause);
057                this.parameter = parameter;
058        }
059
060
061        /**
062         * Return the {@code MethodParameter} associated with this error, if any.
063         */
064        @Nullable
065        public MethodParameter getMethodParameter() {
066                return this.parameter;
067        }
068
069}