#2 -- first implementations of date generator, modeling and forecasting
This commit is contained in:
parent
84a228b06d
commit
be9a761f0f
59
src/main/java/ru/ulstu/controllers/AdviceController.java
Normal file
59
src/main/java/ru/ulstu/controllers/AdviceController.java
Normal file
@ -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 <E> ResponseExtended<E> handleException(ErrorConstants error, E errorData) {
|
||||
log.warn(error.toString());
|
||||
return new ResponseExtended<>(error, errorData);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseExtended<String> handleUnknownException(Throwable e) {
|
||||
e.printStackTrace();
|
||||
return handleException(ErrorConstants.UNKNOWN, e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpServerErrorException.class)
|
||||
public ResponseExtended<String> handleHttpClientException(Throwable e) {
|
||||
return handleException(ErrorConstants.HTTP_CLIENT_ERROR, e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(TimeSeriesValidateException.class)
|
||||
public ResponseExtended<String> handleTimeSeriesValidateException(Throwable e) {
|
||||
return handleException(ErrorConstants.TIME_SERIES_VALIDATE_ERROR, e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(ForecastValidateException.class)
|
||||
public ResponseExtended<String> handleForecastValidateException(Throwable e) {
|
||||
return handleException(ErrorConstants.FORECAST_PARAMS_ERROR, e.getMessage());
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package ru.ulstu.models.response;
|
||||
|
||||
class ControllerResponse<D, E> {
|
||||
private final D data;
|
||||
private final ControllerResponseError<E> error;
|
||||
|
||||
ControllerResponse(D data) {
|
||||
this.data = data;
|
||||
this.error = null;
|
||||
}
|
||||
|
||||
ControllerResponse(ControllerResponseError<E> error) {
|
||||
this.data = null;
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public D getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public ControllerResponseError<E> getError() {
|
||||
return error;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ru.ulstu.models.response;
|
||||
|
||||
class ControllerResponseError<D> {
|
||||
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;
|
||||
}
|
||||
}
|
29
src/main/java/ru/ulstu/models/response/ErrorConstants.java
Normal file
29
src/main/java/ru/ulstu/models/response/ErrorConstants.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
15
src/main/java/ru/ulstu/models/response/Response.java
Normal file
15
src/main/java/ru/ulstu/models/response/Response.java
Normal file
@ -0,0 +1,15 @@
|
||||
package ru.ulstu.models.response;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
public class Response<D> extends ResponseEntity<Object> {
|
||||
|
||||
public Response(D data) {
|
||||
super(new ControllerResponse<D, Void>(data), HttpStatus.OK);
|
||||
}
|
||||
|
||||
public Response(ErrorConstants error) {
|
||||
super(new ControllerResponse<Void, Void>(new ControllerResponseError<>(error, null)), HttpStatus.OK);
|
||||
}
|
||||
}
|
11
src/main/java/ru/ulstu/models/response/ResponseExtended.java
Normal file
11
src/main/java/ru/ulstu/models/response/ResponseExtended.java
Normal file
@ -0,0 +1,11 @@
|
||||
package ru.ulstu.models.response;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
public class ResponseExtended<E> extends ResponseEntity<Object> {
|
||||
|
||||
public ResponseExtended(ErrorConstants error, E errorData) {
|
||||
super(new ControllerResponse<Void, E>(new ControllerResponseError<E>(error, errorData)), HttpStatus.OK);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user