feat: add entity class ApiError and ExceptionApiHandler

This commit is contained in:
Petr Rudichev 2025-02-19 11:31:40 +03:00
parent 90aaab219d
commit 8f094e4800
2 changed files with 50 additions and 0 deletions

View File

@ -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
);
}
}

View File

@ -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")
}