|
| 1 | +package org.namul.api.payload.webmvc.error; |
| 2 | + |
| 3 | +import org.namul.api.payload.core.code.BaseErrorCode; |
| 4 | +import org.namul.api.payload.core.error.AdditionalExceptionHandler; |
| 5 | +import org.namul.api.payload.core.error.ErrorCodeExceptionHandlerConfigurer; |
| 6 | +import org.namul.api.payload.core.writer.FailureResponseWriter; |
| 7 | +import org.springframework.beans.TypeMismatchException; |
| 8 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 9 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 10 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 11 | +import org.springframework.web.bind.MissingPathVariableException; |
| 12 | +import org.springframework.web.bind.MissingServletRequestParameterException; |
| 13 | +import org.springframework.web.servlet.resource.NoResourceFoundException; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.Executor; |
| 17 | + |
| 18 | +public class HttpServletErrorCodeExceptionHandlerConfigurer<T extends BaseErrorCode> extends ErrorCodeExceptionHandlerConfigurer<T> { |
| 19 | + |
| 20 | + public HttpServletErrorCodeExceptionHandlerConfigurer(FailureResponseWriter<T> failureResponseWriter) { |
| 21 | + super(failureResponseWriter); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * The method for Default configuration it contains MethodArgumentNotValid, HttpMessageNotReadable, HttpRequestMethodNotSupported, MissingPathVariable, MissingServletRequestParameter, NoResourceFound, TypeMismatch, ServerApplication and Exception |
| 26 | + * it set Exception to have internalServerErrorCode and other Exceptions to have badRequestError |
| 27 | + * @param badRequestError The BaseErrorCode that can return ErrorReasonDTO for bad request error |
| 28 | + * @param internalServerError The BaseErrorCode that can return ErrorReasonDTO for internal server error |
| 29 | + */ |
| 30 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> withDefault(T badRequestError, |
| 31 | + T internalServerError) { |
| 32 | + return this |
| 33 | + .addMethodArgumentNotValid(badRequestError) |
| 34 | + .addHttpMessageNotReadable(badRequestError) |
| 35 | + .addHttpRequestMethodNotSupported(badRequestError) |
| 36 | + .addMissingPathVariable(badRequestError) |
| 37 | + .addMissingServletRequestParameter(badRequestError) |
| 38 | + .addNoResourceFound(badRequestError) |
| 39 | + .addTypeMismatch(badRequestError) |
| 40 | + .addServerApplication(badRequestError) |
| 41 | + .addGlobalException(internalServerError); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * The method that add ErrorReasonDTO with default handler about MethodArgumentNotValidException |
| 46 | + * @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response |
| 47 | + */ |
| 48 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addMethodArgumentNotValid(T code) { |
| 49 | + return this.addAdvice(MethodArgumentNotValidException.class, code); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * The method that add ErrorReasonDTO with default handler about HttpMessageNotReadableException |
| 54 | + * @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response |
| 55 | + */ |
| 56 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addHttpMessageNotReadable(T code) { |
| 57 | + return this.addAdvice(HttpMessageNotReadableException.class, code); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * The method that add ErrorReasonDTO with default handler about HttpRequestMethodNotSupported |
| 62 | + * @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response |
| 63 | + */ |
| 64 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addHttpRequestMethodNotSupported(T code) { |
| 65 | + return this.addAdvice(HttpRequestMethodNotSupportedException.class, code); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * The method that add ErrorReasonDTO with default handler about MissingPathVariableException |
| 70 | + * @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response |
| 71 | + */ |
| 72 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addMissingPathVariable(T code) { |
| 73 | + return this.addAdvice(MissingPathVariableException.class, code); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * The method that add ErrorReasonDTO with default handler about MissingServletRequestParameterException |
| 78 | + * @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response |
| 79 | + */ |
| 80 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addMissingServletRequestParameter(T code) { |
| 81 | + return this.addAdvice(MissingServletRequestParameterException.class, code); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * The method that add ErrorReasonDTO with default handler about NoResourceFoundException |
| 86 | + * @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response |
| 87 | + */ |
| 88 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addNoResourceFound(T code) { |
| 89 | + return this.addAdvice(NoResourceFoundException.class, code); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * The method that add ErrorReasonDTO with default handler about TypeMismatchException |
| 94 | + * @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response |
| 95 | + */ |
| 96 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addTypeMismatch(T code) { |
| 97 | + return this.addAdvice(TypeMismatchException.class, code); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public <E extends Throwable> HttpServletErrorCodeExceptionHandlerConfigurer<T> addAdvice(Class<E> cls, T code) { |
| 102 | + super.addAdvice(cls, code); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addAdditionalExceptionHandler(AdditionalExceptionHandler<T> additionalExceptionHandler) { |
| 108 | + super.addAdditionalExceptionHandler(additionalExceptionHandler); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addAdditionalExceptionHandlers(List<AdditionalExceptionHandler<T>> additionalExceptionHandlers) { |
| 114 | + super.addAdditionalExceptionHandlers(additionalExceptionHandlers); |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addGlobalException(T code) { |
| 120 | + super.addGlobalException(code); |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> addServerApplication(T code) { |
| 126 | + super.addServerApplication(code); |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public <E extends Throwable> HttpServletErrorCodeExceptionHandlerConfigurer<T> deleteAdvice(Class<E> cls) { |
| 132 | + super.deleteAdvice(cls); |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public HttpServletErrorCodeExceptionHandlerConfigurer<T> setAdditionalExceptionHandlersExecutor(Executor executor) { |
| 138 | + super.setAdditionalExceptionHandlersExecutor(executor); |
| 139 | + return this; |
| 140 | + } |
| 141 | +} |
0 commit comments