diff --git a/src/main/java/ru/ulstu/controllers/AdviceController.java b/src/main/java/ru/ulstu/controllers/AdviceController.java new file mode 100644 index 0000000..de504d1 --- /dev/null +++ b/src/main/java/ru/ulstu/controllers/AdviceController.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2020. Anton Romanov + * 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 ru.ulstu.controllers; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.HttpServerErrorException; +import ru.ulstu.models.exceptions.ForecastValidateException; +import ru.ulstu.models.exceptions.TimeSeriesValidateException; +import ru.ulstu.models.response.ErrorConstants; +import ru.ulstu.models.response.ResponseExtended; + +@RestController +@ControllerAdvice +public class AdviceController { + private final Logger log = LoggerFactory.getLogger(AdviceController.class); + + private ResponseExtended handleException(ErrorConstants error, E errorData) { + log.warn(error.toString()); + return new ResponseExtended<>(error, errorData); + } + + @ExceptionHandler(Exception.class) + public ResponseExtended handleUnknownException(Throwable e) { + e.printStackTrace(); + return handleException(ErrorConstants.UNKNOWN, e.getMessage()); + } + + @ExceptionHandler(HttpServerErrorException.class) + public ResponseExtended handleHttpClientException(Throwable e) { + return handleException(ErrorConstants.HTTP_CLIENT_ERROR, e.getMessage()); + } + + @ExceptionHandler(TimeSeriesValidateException.class) + public ResponseExtended handleTimeSeriesValidateException(Throwable e) { + return handleException(ErrorConstants.TIME_SERIES_VALIDATE_ERROR, e.getMessage()); + } + + @ExceptionHandler(ForecastValidateException.class) + public ResponseExtended handleForecastValidateException(Throwable e) { + return handleException(ErrorConstants.FORECAST_PARAMS_ERROR, e.getMessage()); + } +} diff --git a/src/main/java/ru/ulstu/models/response/ControllerResponse.java b/src/main/java/ru/ulstu/models/response/ControllerResponse.java new file mode 100644 index 0000000..fa3d13a --- /dev/null +++ b/src/main/java/ru/ulstu/models/response/ControllerResponse.java @@ -0,0 +1,24 @@ +package ru.ulstu.models.response; + +class ControllerResponse { + private final D data; + private final ControllerResponseError error; + + ControllerResponse(D data) { + this.data = data; + this.error = null; + } + + ControllerResponse(ControllerResponseError error) { + this.data = null; + this.error = error; + } + + public D getData() { + return data; + } + + public ControllerResponseError getError() { + return error; + } +} diff --git a/src/main/java/ru/ulstu/models/response/ControllerResponseError.java b/src/main/java/ru/ulstu/models/response/ControllerResponseError.java new file mode 100644 index 0000000..b4fa31c --- /dev/null +++ b/src/main/java/ru/ulstu/models/response/ControllerResponseError.java @@ -0,0 +1,23 @@ +package ru.ulstu.models.response; + +class ControllerResponseError { + private final ErrorConstants description; + private final D data; + + ControllerResponseError(ErrorConstants description, D data) { + this.description = description; + this.data = data; + } + + public int getCode() { + return description.getCode(); + } + + public String getMessage() { + return description.getMessage(); + } + + public D getData() { + return data; + } +} diff --git a/src/main/java/ru/ulstu/models/response/ErrorConstants.java b/src/main/java/ru/ulstu/models/response/ErrorConstants.java new file mode 100644 index 0000000..44c6bdf --- /dev/null +++ b/src/main/java/ru/ulstu/models/response/ErrorConstants.java @@ -0,0 +1,29 @@ +package ru.ulstu.models.response; + +public enum ErrorConstants { + UNKNOWN(0, "Unknown error"), + TIME_SERIES_VALIDATE_ERROR(10, "Некорректный временной ряд"), + FORECAST_PARAMS_ERROR(11, "Некорректные параметры для прогнозирования"), + HTTP_CLIENT_ERROR(66, "Http client error"); + + private final int code; + private final String message; + + ErrorConstants(int code, String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } + + @Override + public String toString() { + return String.format("%d: %s", code, message); + } +} diff --git a/src/main/java/ru/ulstu/models/response/Response.java b/src/main/java/ru/ulstu/models/response/Response.java new file mode 100644 index 0000000..fa5ba67 --- /dev/null +++ b/src/main/java/ru/ulstu/models/response/Response.java @@ -0,0 +1,15 @@ +package ru.ulstu.models.response; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public class Response extends ResponseEntity { + + public Response(D data) { + super(new ControllerResponse(data), HttpStatus.OK); + } + + public Response(ErrorConstants error) { + super(new ControllerResponse(new ControllerResponseError<>(error, null)), HttpStatus.OK); + } +} diff --git a/src/main/java/ru/ulstu/models/response/ResponseExtended.java b/src/main/java/ru/ulstu/models/response/ResponseExtended.java new file mode 100644 index 0000000..c79be44 --- /dev/null +++ b/src/main/java/ru/ulstu/models/response/ResponseExtended.java @@ -0,0 +1,11 @@ +package ru.ulstu.models.response; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public class ResponseExtended extends ResponseEntity { + + public ResponseExtended(ErrorConstants error, E errorData) { + super(new ControllerResponse(new ControllerResponseError(error, errorData)), HttpStatus.OK); + } +}