mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-05-08 11:47:28 +08:00
Merge pull request #341 from nagisa77/codex/fix-occasional-errors-in-application
fix: guard against null exception messages
This commit is contained in:
@@ -7,30 +7,40 @@ import com.openisle.exception.FieldException;
|
|||||||
import com.openisle.exception.NotFoundException;
|
import com.openisle.exception.NotFoundException;
|
||||||
import com.openisle.exception.RateLimitException;
|
import com.openisle.exception.RateLimitException;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler(FieldException.class)
|
@ExceptionHandler(FieldException.class)
|
||||||
public ResponseEntity<?> handleFieldException(FieldException ex) {
|
public ResponseEntity<?> handleFieldException(FieldException ex) {
|
||||||
return ResponseEntity.badRequest()
|
Map<String, Object> body = new HashMap<>();
|
||||||
.body(Map.of("error", ex.getMessage(), "field", ex.getField()));
|
body.put("error", Objects.toString(ex.getMessage(), null));
|
||||||
|
body.put("field", ex.getField());
|
||||||
|
return ResponseEntity.badRequest().body(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(NotFoundException.class)
|
@ExceptionHandler(NotFoundException.class)
|
||||||
public ResponseEntity<?> handleNotFoundException(NotFoundException ex) {
|
public ResponseEntity<?> handleNotFoundException(NotFoundException ex) {
|
||||||
return ResponseEntity.status(404).body(Map.of("error", ex.getMessage()));
|
Map<String, Object> body = new HashMap<>();
|
||||||
|
body.put("error", Objects.toString(ex.getMessage(), null));
|
||||||
|
return ResponseEntity.status(404).body(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(RateLimitException.class)
|
@ExceptionHandler(RateLimitException.class)
|
||||||
public ResponseEntity<?> handleRateLimitException(RateLimitException ex) {
|
public ResponseEntity<?> handleRateLimitException(RateLimitException ex) {
|
||||||
return ResponseEntity.status(429).body(Map.of("error", ex.getMessage()));
|
Map<String, Object> body = new HashMap<>();
|
||||||
|
body.put("error", Objects.toString(ex.getMessage(), null));
|
||||||
|
return ResponseEntity.status(429).body(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseEntity<?> handleException(Exception ex) {
|
public ResponseEntity<?> handleException(Exception ex) {
|
||||||
return ResponseEntity.badRequest().body(Map.of("error", ex.getMessage()));
|
Map<String, Object> body = new HashMap<>();
|
||||||
|
body.put("error", Objects.toString(ex.getMessage(), null));
|
||||||
|
return ResponseEntity.badRequest().body(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user