mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-18 13:01:02 +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.RateLimitException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(FieldException.class)
|
||||
public ResponseEntity<?> handleFieldException(FieldException ex) {
|
||||
return ResponseEntity.badRequest()
|
||||
.body(Map.of("error", ex.getMessage(), "field", ex.getField()));
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("error", Objects.toString(ex.getMessage(), null));
|
||||
body.put("field", ex.getField());
|
||||
return ResponseEntity.badRequest().body(body);
|
||||
}
|
||||
|
||||
@ExceptionHandler(NotFoundException.class)
|
||||
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)
|
||||
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)
|
||||
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