How do I get my own Excpetion into the client?

35 views
Skip to first unread message

cmp...@zfin.org

unread,
Nov 20, 2018, 10:14:44 AM11/20/18
to RestyGWT
When I throw my own Exception in my controller class somehow, Restygwt seems to ignore this and return a 200 HTTP status code.
If I set my status code to 500 I get a FailedResponseException in the client I cannot inject an error message.
How can I throw an exception and have the client code pick  up that exception with my error message?

Thanks.

Stanislav Spiridonov

unread,
Nov 22, 2018, 2:30:35 AM11/22/18
to RestyGWT
Hi, 

There is no simple way to do it. In my applications, I use the HTTP codes for exceptions from backend that should be processed on frontend along with serialized exception in the response body.

1. All such exception inherited from a base class - GwtException
@JsonIgnoreProperties({ "cause", "stackTrace", "localizedMessage", "suppressed" })
public class GwtException extends RuntimeException {

private static final long serialVersionUID = -2385815955673412683L;

public enum ErrorCode {
GWT_EXCEPTION(500),
ACCESS_DENIDED_REST_EXCEPTION(403),
ANONYMOUS_USER_REST_EXCEPTION(600),
//...oters
EXPIRED_AUTH_TOKEN_EXCEPTION(609),
                ;

private final int code;

private ErrorCode(final int code) {
this.code = code;
}

public int getCode() {
return code;
}

public static ErrorCode find(final int code) {
final ErrorCode[] values = ErrorCode.values();
for (final ErrorCode value : values) {
if (value.getCode() == code) {
return value;
}
}
return GWT_EXCEPTION;
}

}

private int code = 500;
private String message;

public GwtException() {
super();
}

public GwtException(final String message) {
super(message);
this.message = message;
}

public GwtException(final Throwable cause) {
super(cause);
}

public GwtException(final String message, final Throwable cause) {
super(message, cause);
this.message = message;
}

public int getCode() {
return code;
}

/**
* @param code
*            the code to set
*/
public void setCode(final int code) {
this.code = code;
}

/**
* @return the message
*/
@Override
public String getMessage() {
return message;
}

/**
* @param message
*            the message to set
*/
public void setMessage(final String message) {
this.message = message;
}

}
2. Methods in controllers simple throws necessary exceptions  
3. Base controller registers the following exception handler. So exception cached and result response contains code and serialized exception in the body  
@ExceptionHandler(GwtException.class)
public ResponseEntity<GwtException> setStatusException(final GwtException ex) {
return ResponseEntity.status(ex.getCode()).body(ex);
}

4. On the client side, I register the exception mapper and process know exceptions.
public class AdminModuleEntryPoint extends AbstractModuleEntryPoint {
    //....
public static interface AnonymousUserRestExceptionMapper extends ObjectMapper<AnonymousUserRestException> {}
private static final AnonymousUserRestExceptionMapper anonymousUserRestExceptionMapper = GWT.create(AnonymousUserRestExceptionMapper.class);
        //.....

public void onModuleLoad() {
// ...
Defaults.setExceptionMapper(new ExceptionMapper() {
@Override
public Throwable createFailedStatusException(final Method method, final Response response) {
final int statusCode = response.getStatusCode();
final ErrorCode errorCode = GwtException.ErrorCode.find(statusCode);
switch (errorCode) {
case ANONYMOUS_USER_REST_EXCEPTION:
return anonymousUserRestExceptionMapper.read(response.getText());
case NO_USER_REST_EXCEPTION:
return noUserRestExceptionMapper.read(response.getText());
                                //.....
default:
return new FailedResponseException(response);
}
}
});
                //......
}



That's all.

S.

Reply all
Reply to author
Forward
0 new messages