Skip to content

Commit dc8d9fe

Browse files
author
Anton Hrytsenko
committed
Improve exception handling.
1 parent 4719a77 commit dc8d9fe

File tree

10 files changed

+142
-301
lines changed

10 files changed

+142
-301
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.hrytsenko</groupId>
77
<artifactId>json-data-spring-boot</artifactId>
8-
<version>0.2.1</version>
8+
<version>0.3.0</version>
99

1010
<url>https://github.com/hrytsenko/json-data-spring-boot</url>
1111
<inceptionYear>2020</inceptionYear>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (C) 2020 Anton Hrytsenko
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.hrytsenko.jsondata.springboot.error;
17+
18+
import com.github.hrytsenko.jsondata.JsonBean;
19+
import lombok.AllArgsConstructor;
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.springframework.core.annotation.Order;
22+
import org.springframework.http.HttpStatus;
23+
import org.springframework.http.MediaType;
24+
import org.springframework.http.ResponseEntity;
25+
import org.springframework.web.bind.annotation.ExceptionHandler;
26+
import org.springframework.web.bind.annotation.RestControllerAdvice;
27+
28+
@RestControllerAdvice
29+
@Order
30+
@Slf4j
31+
@AllArgsConstructor
32+
class ExceptionAdvice {
33+
34+
CorrelationSource correlationSource;
35+
36+
@ExceptionHandler(Exception.class)
37+
ResponseEntity<?> onUnexpectedError(Exception exception) {
38+
log.error("Unexpected error", exception);
39+
return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "INTERNAL_ERROR");
40+
}
41+
42+
@ExceptionHandler(ServiceException.BadRequest.class)
43+
ResponseEntity<?> onBadRequest(ServiceException.BadRequest exception) {
44+
log.error("Bad request", exception);
45+
return errorResponse(HttpStatus.BAD_REQUEST, exception.getCode());
46+
}
47+
48+
@ExceptionHandler(ServiceException.Unauthorized.class)
49+
ResponseEntity<?> onUnauthorized(ServiceException.Unauthorized exception) {
50+
log.error("Unauthorized", exception);
51+
return errorResponse(HttpStatus.UNAUTHORIZED, exception.getCode());
52+
}
53+
54+
@ExceptionHandler(ServiceException.Forbidden.class)
55+
ResponseEntity<?> onForbidden(ServiceException.Forbidden exception) {
56+
log.error("Forbidden", exception);
57+
return errorResponse(HttpStatus.FORBIDDEN, exception.getCode());
58+
}
59+
60+
@ExceptionHandler(ServiceException.NotFound.class)
61+
ResponseEntity<?> onNotFound(ServiceException.NotFound exception) {
62+
log.error("Not found", exception);
63+
return errorResponse(HttpStatus.NOT_FOUND, exception.getCode());
64+
}
65+
66+
@ExceptionHandler(ServiceException.InternalError.class)
67+
ResponseEntity<?> onInternalError(ServiceException.InternalError exception) {
68+
log.error("Internal error", exception);
69+
return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getCode());
70+
}
71+
72+
@ExceptionHandler(ServiceException.ServiceUnavailable.class)
73+
ResponseEntity<?> onServiceUnavailable(ServiceException.ServiceUnavailable exception) {
74+
log.error("Service unavailable", exception);
75+
return errorResponse(HttpStatus.SERVICE_UNAVAILABLE, exception.getCode());
76+
}
77+
78+
private ResponseEntity<?> errorResponse(HttpStatus status, String code) {
79+
return ResponseEntity.status(status)
80+
.contentType(MediaType.APPLICATION_JSON)
81+
.body(new JsonBean()
82+
.putString("error.correlation", correlationSource.getCorrelation())
83+
.putString("error.code", code));
84+
}
85+
86+
}

src/main/java/com/github/hrytsenko/jsondata/springboot/error/ExceptionAdvices.java

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

src/main/java/com/github/hrytsenko/jsondata/springboot/web/ValidateRequestAspect.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717

1818
import com.github.hrytsenko.jsondata.JsonEntity;
1919
import com.github.hrytsenko.jsondata.JsonValidatorException;
20+
import com.github.hrytsenko.jsondata.springboot.error.ServiceException;
2021
import lombok.AllArgsConstructor;
2122
import org.aspectj.lang.JoinPoint;
2223
import org.aspectj.lang.annotation.Aspect;
2324
import org.aspectj.lang.annotation.Before;
2425
import org.springframework.context.annotation.Configuration;
25-
import org.springframework.core.annotation.Order;
2626

2727
@Aspect
28-
@Order(0)
2928
@Configuration
3029
@AllArgsConstructor
3130
class ValidateRequestAspect {
@@ -41,7 +40,7 @@ void handle(JoinPoint point, ValidateRequest config) {
4140
validatorSource.getValidator(schemaName)
4241
.validate(target);
4342
} catch (JsonValidatorException exception) {
44-
throw new ValidateRequestException("Invalid request " + schemaName, exception);
43+
throw new ServiceException.BadRequest("INVALID_REQUEST", exception);
4544
}
4645
}
4746

src/main/java/com/github/hrytsenko/jsondata/springboot/web/ValidateRequestException.java

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

src/main/java/com/github/hrytsenko/jsondata/springboot/web/ValidateResponseAspect.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717

1818
import com.github.hrytsenko.jsondata.JsonEntity;
1919
import com.github.hrytsenko.jsondata.JsonValidatorException;
20+
import com.github.hrytsenko.jsondata.springboot.error.ServiceException;
2021
import lombok.AllArgsConstructor;
2122
import lombok.SneakyThrows;
2223
import org.aspectj.lang.ProceedingJoinPoint;
2324
import org.aspectj.lang.annotation.Around;
2425
import org.aspectj.lang.annotation.Aspect;
2526
import org.springframework.context.annotation.Configuration;
26-
import org.springframework.core.annotation.Order;
2727

2828
@Aspect
29-
@Order(0)
3029
@Configuration
3130
@AllArgsConstructor
3231
class ValidateResponseAspect {
@@ -43,7 +42,7 @@ Object handle(ProceedingJoinPoint point, ValidateResponse config) {
4342
validatorSource.getValidator(config.value())
4443
.validate(target);
4544
} catch (JsonValidatorException exception) {
46-
throw new ValidateResponseException("Invalid response " + schemaName, exception);
45+
throw new ServiceException.InternalError("INVALID_RESPONSE", exception);
4746
}
4847

4948
return target;

src/main/java/com/github/hrytsenko/jsondata/springboot/web/ValidateResponseException.java

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

0 commit comments

Comments
 (0)