Refine encoding/decoding exception handling

Starting with removing a package cycle on the use of
ResponseStatusException in the codec package, this commit generally
refines codec exception handling.

The new [Encoding|Decoding]Exception mirror the existing
HttpMessageNot[Readable|Writable]Exception and are used similarly
especially to differentiate betwen 400 and 500 errors when parsing
server request body content.

The commit also aligns some of the exception handling of JSON and XML
on the WebFlux side with that on the Spring MVC side.

Issue: SPR-15516
This commit is contained in:
Rossen Stoyanchev
2017-05-05 14:22:27 -04:00
parent d7e54cea84
commit 83e0e1604a
12 changed files with 147 additions and 87 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,18 +19,29 @@ package org.springframework.core.codec;
import org.springframework.core.NestedRuntimeException;
/**
* Codec related exception, usually used as a wrapper for a cause exception.
* General error that indicates a problem while encoding and decoding to and
* from an Object stream.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @since 5.0
*/
@SuppressWarnings("serial")
public class CodecException extends NestedRuntimeException {
/**
* Create a new CodecException.
* @param msg the detail message
*/
public CodecException(String msg) {
super(msg);
}
/**
* Create a new CodecException.
* @param msg the detail message
* @param cause root cause for the exception, if any
*/
public CodecException(String msg, Throwable cause) {
super(msg, cause);
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.codec;
/**
* Indicates an issue with decoding the input stream with the focus on indicating
* a content issue such as a parse failure. As opposed to a more general I/O
* errors, illegal state, or a {@link CodecException} such as a configuration
* issue that a {@link Decoder} may choose to raise.
*
* <p>For example in a web application, a server side {@code DecodingException}
* would translate to a response with a 400 (bad input) status while
* {@code CodecException} would translate to 500 (server error) status.
*
* @author Rossen Stoyanchev
* @since 5.0
* @see Decoder
*/
@SuppressWarnings("serial")
public class DecodingException extends CodecException {
/**
* Create a new DecodingException.
* @param msg the detail message
*/
public DecodingException(String msg) {
super(msg);
}
/**
* Create a new DecodingException.
* @param msg the detail message
* @param cause root cause for the exception, if any
*/
public DecodingException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -13,24 +13,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.codec;
/**
* Codec exception suitable for internal errors, like those not related to invalid data. It can be used to make sure
* such error will produce a 5xx status code and not a 4xx one when reading HTTP messages for example.
* Indicates an issue with encoding the input Object stream with a focus on
* indicating the Objects cannot be encoded. As opposed to a more general
* {@link CodecException} such as a configuration issue that an {@link Encoder}
* may also choose to raise.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @since 5.0
* @see Encoder
*/
@SuppressWarnings("serial")
public class InternalCodecException extends CodecException {
public class EncodingException extends CodecException {
public InternalCodecException(String msg) {
/**
* Create a new EncodingException.
* @param msg the detail message
*/
public EncodingException(String msg) {
super(msg);
}
public InternalCodecException(String msg, Throwable cause) {
/**
* Create a new EncodingException.
* @param msg the detail message
* @param cause root cause for the exception, if any
*/
public EncodingException(String msg, Throwable cause) {
super(msg, cause);
}