3232import javax .ws .rs .ext .Provider ;
3333import java .io .PrintWriter ;
3434import java .io .StringWriter ;
35+ import java .lang .reflect .InvocationTargetException ;
36+ import java .lang .reflect .UndeclaredThrowableException ;
37+ import java .util .Objects ;
3538
3639/**
3740 *
@@ -66,6 +69,20 @@ public Response toResponse(Throwable ex) {
6669 ex = new RuntimeException (ex .getMessage ());
6770 } else if (ex instanceof ConceptNotExistException ) {
6871 responseStatus = Status .BAD_REQUEST ;
72+ } else if (ex instanceof UndeclaredThrowableException ) {
73+ Throwable throwable = getThrowable ((UndeclaredThrowableException )ex );
74+ if (Objects .nonNull (throwable )) {
75+ if (throwable instanceof ConceptNotExistException ) {
76+ responseStatus = Status .BAD_REQUEST ;
77+ ex = throwable ;
78+ } else {
79+ responseStatus = Status .INTERNAL_SERVER_ERROR ;
80+ ex = new RuntimeException ("An exception occurred: " + ex .getClass ().getName ());
81+ }
82+ } else {
83+ responseStatus = Status .INTERNAL_SERVER_ERROR ;
84+ ex = new RuntimeException ("An exception occurred: " + ex .getClass ().getName ());
85+ }
6986 } else {
7087 responseStatus = Status .INTERNAL_SERVER_ERROR ;
7188 // Create new message to prevent sending error information to client
@@ -79,4 +96,12 @@ public Response toResponse(Throwable ex) {
7996 .type (MediaType .APPLICATION_JSON )
8097 .build ();
8198 }
99+
100+ private Throwable getThrowable (UndeclaredThrowableException ex ) {
101+ if (Objects .nonNull (ex .getUndeclaredThrowable ()) && ex .getUndeclaredThrowable () instanceof InvocationTargetException ) {
102+ InvocationTargetException ite = (InvocationTargetException ) ex .getUndeclaredThrowable ();
103+ return ite .getTargetException ();
104+ }
105+ return null ;
106+ }
82107}
0 commit comments