feat: add entity class ApiError and ExceptionApiHandler
This commit is contained in:
parent
90aaab219d
commit
8f094e4800
@ -0,0 +1,36 @@
|
||||
package com.example.nto.domain.exception.advice;
|
||||
|
||||
import com.amazonaws.SdkBaseException;
|
||||
import com.amazonaws.SdkClientException;
|
||||
import com.amazonaws.services.s3.model.AmazonS3Exception;
|
||||
import com.example.nto.domain.exception.model.ApiError;
|
||||
import com.example.nto.utils.Utils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static com.example.nto.utils.DataFormatType.DATE_TIME;
|
||||
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class ExceptionApiHandler {
|
||||
@ExceptionHandler({AmazonS3Exception.class, SdkClientException.class})
|
||||
public ResponseEntity<ApiError> amazonS3Exception(SdkBaseException e) {
|
||||
return new ResponseEntity<>(
|
||||
new ApiError("500", "Что-то пошло не так с AWS SDK.", e.getMessage(), Utils.nowTime(DATE_TIME)),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
@ExceptionHandler({InterruptedException.class, ExecutionException.class})
|
||||
public ResponseEntity<ApiError> runtimeException(RuntimeException e) {
|
||||
return new ResponseEntity<>(
|
||||
new ApiError("500", "Один из потоков завершился исключением.", e.getMessage(), Utils.nowTime(DATE_TIME)),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.example.nto.domain.exception.model;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ApiError {
|
||||
private String status; // Код статуса HTTP-ответа
|
||||
private String reason; // Общее описание причины ошибки
|
||||
private String message; // Сообщение об ошибке
|
||||
private String timestamp; // Дата и время когда произошла ошибка (в формате "yyyy-MM-dd HH:mm:ss")
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user