Merge pull request #341 from nagisa77/codex/fix-occasional-errors-in-application

fix: guard against null exception messages
This commit is contained in:
Tim
2025-08-04 14:23:41 +08:00
committed by GitHub

View File

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