|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Anton Hrytsenko |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.hrytsenko.jsondata.springboot.error; |
| 17 | + |
| 18 | +import com.github.hrytsenko.jsondata.JsonBean; |
| 19 | +import lombok.AllArgsConstructor; |
| 20 | +import lombok.extern.slf4j.Slf4j; |
| 21 | +import org.springframework.core.annotation.Order; |
| 22 | +import org.springframework.http.HttpStatus; |
| 23 | +import org.springframework.http.MediaType; |
| 24 | +import org.springframework.http.ResponseEntity; |
| 25 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 26 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 27 | + |
| 28 | +@RestControllerAdvice |
| 29 | +@Order |
| 30 | +@Slf4j |
| 31 | +@AllArgsConstructor |
| 32 | +class ExceptionAdvice { |
| 33 | + |
| 34 | + CorrelationSource correlationSource; |
| 35 | + |
| 36 | + @ExceptionHandler(Exception.class) |
| 37 | + ResponseEntity<?> onUnexpectedError(Exception exception) { |
| 38 | + log.error("Unexpected error", exception); |
| 39 | + return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "INTERNAL_ERROR"); |
| 40 | + } |
| 41 | + |
| 42 | + @ExceptionHandler(ServiceException.BadRequest.class) |
| 43 | + ResponseEntity<?> onBadRequest(ServiceException.BadRequest exception) { |
| 44 | + log.error("Bad request", exception); |
| 45 | + return errorResponse(HttpStatus.BAD_REQUEST, exception.getCode()); |
| 46 | + } |
| 47 | + |
| 48 | + @ExceptionHandler(ServiceException.Unauthorized.class) |
| 49 | + ResponseEntity<?> onUnauthorized(ServiceException.Unauthorized exception) { |
| 50 | + log.error("Unauthorized", exception); |
| 51 | + return errorResponse(HttpStatus.UNAUTHORIZED, exception.getCode()); |
| 52 | + } |
| 53 | + |
| 54 | + @ExceptionHandler(ServiceException.Forbidden.class) |
| 55 | + ResponseEntity<?> onForbidden(ServiceException.Forbidden exception) { |
| 56 | + log.error("Forbidden", exception); |
| 57 | + return errorResponse(HttpStatus.FORBIDDEN, exception.getCode()); |
| 58 | + } |
| 59 | + |
| 60 | + @ExceptionHandler(ServiceException.NotFound.class) |
| 61 | + ResponseEntity<?> onNotFound(ServiceException.NotFound exception) { |
| 62 | + log.error("Not found", exception); |
| 63 | + return errorResponse(HttpStatus.NOT_FOUND, exception.getCode()); |
| 64 | + } |
| 65 | + |
| 66 | + @ExceptionHandler(ServiceException.InternalError.class) |
| 67 | + ResponseEntity<?> onInternalError(ServiceException.InternalError exception) { |
| 68 | + log.error("Internal error", exception); |
| 69 | + return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getCode()); |
| 70 | + } |
| 71 | + |
| 72 | + @ExceptionHandler(ServiceException.ServiceUnavailable.class) |
| 73 | + ResponseEntity<?> onServiceUnavailable(ServiceException.ServiceUnavailable exception) { |
| 74 | + log.error("Service unavailable", exception); |
| 75 | + return errorResponse(HttpStatus.SERVICE_UNAVAILABLE, exception.getCode()); |
| 76 | + } |
| 77 | + |
| 78 | + private ResponseEntity<?> errorResponse(HttpStatus status, String code) { |
| 79 | + return ResponseEntity.status(status) |
| 80 | + .contentType(MediaType.APPLICATION_JSON) |
| 81 | + .body(new JsonBean() |
| 82 | + .putString("error.correlation", correlationSource.getCorrelation()) |
| 83 | + .putString("error.code", code)); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments