Skip to content

Commit de03621

Browse files
committed
cleanup work
1 parent 09244a0 commit de03621

15 files changed

Lines changed: 442 additions & 756 deletions

File tree

‎m2-lesson1-start/common/src/main/java/com/baeldung/common/web/RestResponseEntityExceptionHandler.java‎

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
package com.baeldung.common.web;
22

3+
import javax.persistence.EntityNotFoundException;
4+
35
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
7+
import org.springframework.dao.DataAccessException;
8+
import org.springframework.dao.InvalidDataAccessApiUsageException;
9+
import org.springframework.http.HttpHeaders;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
512
import org.springframework.web.bind.annotation.ControllerAdvice;
13+
import org.springframework.web.bind.annotation.ExceptionHandler;
14+
import org.springframework.web.context.request.WebRequest;
615
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
716

17+
import com.baeldung.common.persistence.exception.MyEntityNotFoundException;
18+
import com.baeldung.common.web.exception.MyConflictException;
19+
import com.baeldung.common.web.exception.MyForbiddenException;
20+
import com.baeldung.common.web.exception.MyPreconditionFailedException;
21+
822
@ControllerAdvice
923
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
10-
11-
private Logger log = LoggerFactory.getLogger(getClass());
24+
25+
private final Logger log = LoggerFactory.getLogger(getClass());
1226

1327
public RestResponseEntityExceptionHandler() {
1428
super();
@@ -17,4 +31,52 @@ public RestResponseEntityExceptionHandler() {
1731
// API
1832

1933
// 400
34+
35+
// ============== generic
36+
37+
// 403
38+
39+
@ExceptionHandler({ MyForbiddenException.class })
40+
public ResponseEntity<Object> handleForbidden(final MyForbiddenException ex, final WebRequest request) {
41+
final String bodyOfResponse = "This should be application specific";
42+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.FORBIDDEN, request);
43+
}
44+
45+
// 404
46+
47+
@ExceptionHandler({ MyEntityNotFoundException.class })
48+
protected ResponseEntity<Object> handleNotFound(final MyEntityNotFoundException ex, final WebRequest request) {
49+
final String bodyOfResponse = "This should be application specific";
50+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
51+
}
52+
53+
@ExceptionHandler(value = { EntityNotFoundException.class })
54+
protected ResponseEntity<Object> handleBadRequest(final EntityNotFoundException ex, final WebRequest request) {
55+
final String bodyOfResponse = "This should be application specific";
56+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
57+
}
58+
59+
// 409
60+
61+
@ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class, MyConflictException.class })
62+
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
63+
final String bodyOfResponse = "This should be application specific";
64+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
65+
}
66+
67+
// 4xx
68+
69+
@ExceptionHandler({ MyPreconditionFailedException.class })
70+
/*412*/protected ResponseEntity<Object> handlePreconditionFailed(final RuntimeException ex, final WebRequest request) {
71+
final String bodyOfResponse = "This should be application specific";
72+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.PRECONDITION_FAILED, request);
73+
}
74+
75+
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
76+
/*500*/public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
77+
logger.error("500 Status Code", ex);
78+
final String bodyOfResponse = "This should be application specific";
79+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
80+
}
81+
2082
}

‎m2-lesson1/common/src/main/java/com/baeldung/common/web/RestResponseEntityExceptionHandler.java‎

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)