Skip to content

Commit c4b697b

Browse files
committed
Added error handling
1 parent 147e989 commit c4b697b

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/main/java/in/erail/route/OpenAPI3RouteBuilder.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Arrays;
3131
import java.util.HashMap;
3232
import java.util.Optional;
33+
import org.apache.commons.lang3.exception.ExceptionUtils;
3334

3435
/**
3536
*
@@ -256,27 +257,34 @@ public Router getRouter(Router pRouter) {
256257
apiFactory.addFailureHandlerByOperationId(service.getOperationId(), (routingContext) -> {
257258
routingContext
258259
.response()
259-
.setStatusCode(400)
260-
.end(routingContext.failure().toString());
260+
.setStatusCode(routingContext.statusCode())
261+
.end(generateErrorResponse(routingContext));
261262
});
262263
apiFactory.setValidationFailureHandler((routingContext) -> {
263264
routingContext
264265
.response()
265-
.setStatusCode(400)
266-
.end(routingContext.failure().toString());
266+
.setStatusCode(routingContext.statusCode())
267+
.end(generateErrorResponse(routingContext));
267268
});
268269
apiFactory.setNotImplementedFailureHandler((routingContext) -> {
269270
routingContext
270271
.response()
271-
.setStatusCode(400)
272-
.end(routingContext.failure().toString());
272+
.setStatusCode(routingContext.statusCode())
273+
.end(generateErrorResponse(routingContext));
273274
});
274275
});
275276
});
276277

277278
return apiFactory.getRouter();
278279
}
279280

281+
protected String generateErrorResponse(RoutingContext pContext) {
282+
return Optional
283+
.ofNullable(pContext.failure())
284+
.map(error -> ExceptionUtils.getMessage(error))
285+
.orElse(pContext.getBodyAsString());
286+
}
287+
280288
public boolean isSecurityEnable() {
281289
return mSecurityEnable;
282290
}

0 commit comments

Comments
 (0)