mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-04-21 11:27:27 +08:00
Return 404 for missing user or post
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.openisle.controller;
|
package com.openisle.controller;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
@@ -16,6 +17,15 @@ public class GlobalExceptionHandler {
|
|||||||
.body(Map.of("error", ex.getMessage(), "field", ex.getField()));
|
.body(Map.of("error", ex.getMessage(), "field", ex.getField()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(IllegalArgumentException.class)
|
||||||
|
public ResponseEntity<?> handleIllegalArgumentException(IllegalArgumentException ex) {
|
||||||
|
String msg = ex.getMessage();
|
||||||
|
if (msg != null && (msg.contains("User not found") || msg.contains("Post not found"))) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("error", msg));
|
||||||
|
}
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("error", msg));
|
||||||
|
}
|
||||||
|
|
||||||
@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()));
|
return ResponseEntity.badRequest().body(Map.of("error", ex.getMessage()));
|
||||||
|
|||||||
Reference in New Issue
Block a user