Skip to content

Commit a21f635

Browse files
committed
Add exception ServiceException.ServiceUnavailable.
1 parent 7cd9c0a commit a21f635

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public ErrorResponse onInternalError(ServiceException.InternalError exception) {
9898
return ErrorResponse.create(exception.getCode(), correlationSource.getCorrelation());
9999
}
100100

101+
@ExceptionHandler(ServiceException.ServiceUnavailable.class)
102+
@ResponseStatus(code = HttpStatus.SERVICE_UNAVAILABLE)
103+
public ErrorResponse onServiceUnavailable(ServiceException.ServiceUnavailable exception) {
104+
log.error("Service unavailable", exception);
105+
return ErrorResponse.create(exception.getCode(), correlationSource.getCorrelation());
106+
}
107+
101108
}
102109

103110
@RestControllerAdvice

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,16 @@ public InternalError(String code, Throwable cause) {
9393

9494
}
9595

96+
public static class ServiceUnavailable extends ServiceException {
97+
98+
public ServiceUnavailable() {
99+
this(null);
100+
}
101+
102+
public ServiceUnavailable(Throwable cause) {
103+
super("SERVICE_UNAVAILABLE", cause);
104+
}
105+
106+
}
107+
96108
}

src/test/java/com/github/hrytsenko/jsondata/springboot/error/ExceptionAdvicesTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ void serviceExceptionAdvice_onInternalError() {
108108
Assertions.assertEquals(expectedResponse, actualResponse);
109109
}
110110

111+
@Test
112+
void serviceExceptionAdvice_onServiceUnavailable() {
113+
ExceptionAdvices.ServiceExceptionAdvice advice = new ExceptionAdvices.ServiceExceptionAdvice(correlationSource);
114+
115+
ServiceException.ServiceUnavailable sourceException = new ServiceException.ServiceUnavailable();
116+
117+
ExceptionAdvices.ErrorResponse actualResponse = advice.onServiceUnavailable(sourceException);
118+
119+
ExceptionAdvices.ErrorResponse expectedResponse = ExceptionAdvices.ErrorResponse.create("SERVICE_UNAVAILABLE", CORRELATION);
120+
Assertions.assertEquals(expectedResponse, actualResponse);
121+
}
122+
111123
@Test
112124
void validateExceptionAdvice_onProcessJson() {
113125
ExceptionAdvices.ValidateExceptionAdvice advice = new ExceptionAdvices.ValidateExceptionAdvice(correlationSource);

0 commit comments

Comments
 (0)