|
| 1 | +/*- |
| 2 | + * ========================LICENSE_START================================= |
| 3 | + * fiberoptics-das-producer |
| 4 | + * %% |
| 5 | + * Copyright (C) 2020 Equinor ASA |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * =========================LICENSE_END================================== |
| 19 | + */ |
| 20 | + |
| 21 | +package com.equinor.fiberoptics.das.remotecontrol; |
| 22 | + |
| 23 | +import jakarta.servlet.http.HttpServletRequest; |
| 24 | +import org.springframework.http.HttpStatus; |
| 25 | +import org.springframework.http.ResponseEntity; |
| 26 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 27 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 28 | + |
| 29 | +/** |
| 30 | + * Maps remote-control errors to structured responses. |
| 31 | + */ |
| 32 | +@RestControllerAdvice(basePackages = "com.equinor.fiberoptics.das.remotecontrol") |
| 33 | +public class RemoteControlExceptionHandler { |
| 34 | + |
| 35 | + @ExceptionHandler(RemoteControlService.BadRequestException.class) |
| 36 | + public ResponseEntity<ErrorResponse> handleBadRequest( |
| 37 | + RemoteControlService.BadRequestException ex, |
| 38 | + HttpServletRequest request) { |
| 39 | + return build(HttpStatus.BAD_REQUEST, "RC-400", ex.getMessage(), request); |
| 40 | + } |
| 41 | + |
| 42 | + @ExceptionHandler(RemoteControlService.UnauthorizedException.class) |
| 43 | + public ResponseEntity<ErrorResponse> handleUnauthorized( |
| 44 | + RemoteControlService.UnauthorizedException ex, |
| 45 | + HttpServletRequest request) { |
| 46 | + return build(HttpStatus.UNAUTHORIZED, "RC-401", "Unauthorized", request); |
| 47 | + } |
| 48 | + |
| 49 | + @ExceptionHandler(RemoteControlService.NotFoundException.class) |
| 50 | + public ResponseEntity<ErrorResponse> handleNotFound( |
| 51 | + RemoteControlService.NotFoundException ex, |
| 52 | + HttpServletRequest request) { |
| 53 | + return build(HttpStatus.NOT_FOUND, "RC-404", ex.getMessage(), request); |
| 54 | + } |
| 55 | + |
| 56 | + @ExceptionHandler(IllegalStateException.class) |
| 57 | + public ResponseEntity<ErrorResponse> handleIllegalState( |
| 58 | + IllegalStateException ex, |
| 59 | + HttpServletRequest request) { |
| 60 | + return build(HttpStatus.INTERNAL_SERVER_ERROR, "RC-500", ex.getMessage(), request); |
| 61 | + } |
| 62 | + |
| 63 | + private ResponseEntity<ErrorResponse> build( |
| 64 | + HttpStatus status, |
| 65 | + String code, |
| 66 | + String message, |
| 67 | + HttpServletRequest request) { |
| 68 | + String path = request == null ? null : request.getRequestURI(); |
| 69 | + String safeMessage = message == null || message.isBlank() ? status.getReasonPhrase() : message; |
| 70 | + return ResponseEntity.status(status).body(ErrorResponse.of(code, safeMessage, path)); |
| 71 | + } |
| 72 | +} |
0 commit comments