diff options
Diffstat (limited to 'src/main/java/org/onap/a1pesimulator/exception')
3 files changed, 86 insertions, 0 deletions
diff --git a/src/main/java/org/onap/a1pesimulator/exception/ApiError.java b/src/main/java/org/onap/a1pesimulator/exception/ApiError.java new file mode 100644 index 0000000..13b2472 --- /dev/null +++ b/src/main/java/org/onap/a1pesimulator/exception/ApiError.java @@ -0,0 +1,30 @@ +package org.onap.a1pesimulator.exception; + +import java.time.LocalDateTime; + +import org.springframework.http.HttpStatus; + +import com.fasterxml.jackson.annotation.JsonFormat; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +class ApiError { + + private HttpStatus status; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") + private LocalDateTime timestamp; + private String message; + private String debugMessage; + + private ApiError() { + timestamp = LocalDateTime.now(); + } + + ApiError(HttpStatus status) { + this(); + this.status = status; + } +}
\ No newline at end of file diff --git a/src/main/java/org/onap/a1pesimulator/exception/CellNotFoundException.java b/src/main/java/org/onap/a1pesimulator/exception/CellNotFoundException.java new file mode 100644 index 0000000..59562a2 --- /dev/null +++ b/src/main/java/org/onap/a1pesimulator/exception/CellNotFoundException.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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.onap.a1pesimulator.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class CellNotFoundException extends RuntimeException { + + public CellNotFoundException(String errorMessage) { + super(errorMessage); + } +} diff --git a/src/main/java/org/onap/a1pesimulator/exception/RestExceptionHandler.java b/src/main/java/org/onap/a1pesimulator/exception/RestExceptionHandler.java new file mode 100644 index 0000000..95ec127 --- /dev/null +++ b/src/main/java/org/onap/a1pesimulator/exception/RestExceptionHandler.java @@ -0,0 +1,31 @@ +package org.onap.a1pesimulator.exception; + +import static org.springframework.http.HttpStatus.NOT_FOUND; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@Order(Ordered.HIGHEST_PRECEDENCE) +@ControllerAdvice +public class RestExceptionHandler extends ResponseEntityExceptionHandler { + + private static final Logger log = LoggerFactory.getLogger(RestExceptionHandler.class); + + @ExceptionHandler(CellNotFoundException.class) + protected ResponseEntity<Object> handleEntityNotFound(CellNotFoundException ex) { + ApiError apiError = new ApiError(NOT_FOUND); + apiError.setMessage(ex.getMessage()); + return buildResponseEntity(apiError); + } + + private ResponseEntity<Object> buildResponseEntity(ApiError apiError) { + log.error(apiError.getMessage()); + return new ResponseEntity<>(apiError.getMessage(), apiError.getStatus()); + } +}
\ No newline at end of file |