From 23812e1149b7b74a005d3bcc4ca7860d2ffa9124 Mon Sep 17 00:00:00 2001 From: dashevchenko Date: Tue, 5 Mar 2024 17:13:53 +0200 Subject: [PATCH] changed type of ThingsboardErrorResponse timestamp from Date to long --- .../ThingsboardErrorController.java | 56 +++++++++++++++++++ .../exception/ThingsboardErrorResponse.java | 6 +- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 application/src/main/java/org/thingsboard/server/controller/ThingsboardErrorController.java diff --git a/application/src/main/java/org/thingsboard/server/controller/ThingsboardErrorController.java b/application/src/main/java/org/thingsboard/server/controller/ThingsboardErrorController.java new file mode 100644 index 0000000000..b70edcba45 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/controller/ThingsboardErrorController.java @@ -0,0 +1,56 @@ +/** + * Copyright © 2016-2024 The Thingsboard Authors + * + * 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.thingsboard.server.controller; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.web.servlet.error.ErrorController; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.RequestMapping; +import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; +import org.thingsboard.server.exception.ThingsboardErrorResponse; + +import javax.servlet.RequestDispatcher; +import javax.servlet.http.HttpServletRequest; + +@Slf4j +@ControllerAdvice +public class ThingsboardErrorController implements ErrorController { + + public static final String PATH_NOT_FOUND_ERROR_DESCRIPTION = "Path is not found."; + public static final String GENERAL_ERROR_DESCRIPTION = "Something went wrong! Our Engineers are on it"; + + @RequestMapping("/error") + public ResponseEntity handleError(HttpServletRequest request) { + Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + + if (status != null) { + int statusCode = Integer.parseInt(status.toString()); + + if(statusCode == HttpStatus.NOT_FOUND.value()) { + return new ResponseEntity<>(ThingsboardErrorResponse.of(PATH_NOT_FOUND_ERROR_DESCRIPTION, ThingsboardErrorCode.ITEM_NOT_FOUND, HttpStatus.NOT_FOUND), httpHeaders, HttpStatus.NOT_FOUND); + } + } + return new ResponseEntity<>(ThingsboardErrorResponse.of(GENERAL_ERROR_DESCRIPTION, ThingsboardErrorCode.GENERAL, HttpStatus.INTERNAL_SERVER_ERROR), httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR); + } + +} diff --git a/application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponse.java b/application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponse.java index 26084ad077..513aff6891 100644 --- a/application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponse.java +++ b/application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponse.java @@ -33,13 +33,13 @@ public class ThingsboardErrorResponse { // Error code private final ThingsboardErrorCode errorCode; - private final Date timestamp; + private final long timestamp; protected ThingsboardErrorResponse(final String message, final ThingsboardErrorCode errorCode, HttpStatus status) { this.message = message; this.errorCode = errorCode; this.status = status; - this.timestamp = new java.util.Date(); + this.timestamp = System.currentTimeMillis(); } public static ThingsboardErrorResponse of(final String message, final ThingsboardErrorCode errorCode, HttpStatus status) { @@ -75,7 +75,7 @@ public class ThingsboardErrorResponse { } @ApiModelProperty(position = 4, value = "Timestamp", accessMode = ApiModelProperty.AccessMode.READ_ONLY) - public Date getTimestamp() { + public long getTimestamp() { return timestamp; } }