cxf-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "n0rad (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (CXF-4496) find of ResponseExceptionMapper do not handle runtime exceptions
Date Tue, 04 Sep 2012 10:38:07 GMT

    [ https://issues.apache.org/jira/browse/CXF-4496?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13447601#comment-13447601
] 

n0rad edited comment on CXF-4496 at 9/4/12 9:38 PM:
----------------------------------------------------

Example that store any kind of runtime and rebuild it on client side : 

{code:java}
public class AjslRuntimeExceptionMapper implements ExceptionMapper<RuntimeException>
{

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public Response toResponse(RuntimeException exception) {
        log.warn("Uncaught RuntimeException", exception);

        Error error = new Error();
        error.setMessage(exception.getMessage());
        error.setErrorClass(exception.getClass());
        return Response.status(500).entity(error).build();
    }
}

public class AjslResponseExceptionMapper implements ResponseExceptionMapper<Exception>
{

    @Override
    public Exception fromResponse(Response r) {
        Error error = findError(r);
        Exception exception;
        if (error.getErrorClass() != null) {
            try {
                exception = error.getErrorClass().getConstructor(String.class)
                        .newInstance(Strings.nullToEmpty(error.getMessage()));
            } catch (Exception e) {
                throw new RuntimeException("Cannot Create Exception from Error : " + error,
e);
            }
        } else {
            exception = new RuntimeException(error.getMessage());
        }
        return exception;
    }
}

{code}
                
      was (Author: n0rad):
    Example that store any kind of runtime and rebuild it on client side : 

{code:java}
public class AjslRuntimeExceptionMapper implements ExceptionMapper<RuntimeException>
{

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public Response toResponse(RuntimeException exception) {
        log.warn("Uncaught RuntimeException", exception);

        Error error = new Error();
        error.setMessage(exception.getMessage());
        error.setErrorClass(exception.getClass());
        return Response.status(500).entity(error).build();
    }
}

public class AjslResponseExceptionMapper implements ResponseExceptionMapper<Exception>
{

    @Override
    public Exception fromResponse(Response r) {
        Error errorModel = findErrorModel(r);
        Exception exception;
        if (errorModel.getErrorClass() != null) {
            try {
                exception = errorModel.getErrorClass().getConstructor(String.class)
                        .newInstance(Strings.nullToEmpty(errorModel.getMessage()));
            } catch (Exception e) {
                throw new RuntimeException("Cannot Create Exception from Error model : " +
errorModel, e);
            }
        } else {
            exception = new RuntimeException(errorModel.getMessage());
        }
        return exception;
    }
}

{code}
                  
> find of ResponseExceptionMapper do not handle runtime exceptions
> ----------------------------------------------------------------
>
>                 Key: CXF-4496
>                 URL: https://issues.apache.org/jira/browse/CXF-4496
>             Project: CXF
>          Issue Type: Bug
>          Components: JAX-RS
>    Affects Versions: 2.6.2
>            Reporter: n0rad
>            Priority: Minor
>
> In org.apache.cxf.jaxrs.client.ClientProxyImpl.findExceptionMapper(Method, Message)
> The responseExceptionMapper is selected based on exception defined in the resource method
signature but this method may have thrown a RuntimeException :
> {code:java} 
> @Path("/")
> public interface RestMcuBoardResource {
>     @GET
>     public RestMcuBoard getBoard();
> }
> {code} 
> does not work where this one work
> {code:java} 
> @Path("/")
> public interface RestMcuBoardResource {
>     @GET
>     public RestMcuBoard getBoard() throws runtimeException;
> }
> {code}
> a fix could be :
> {code:java} 
>     private static ResponseExceptionMapper<?> findExceptionMapper(Method m, Message
message) {
>         ProviderFactory pf = ProviderFactory.getInstance(message);
>         for (Class<?> exType : m.getExceptionTypes()) {
>             ResponseExceptionMapper<?> mapper = pf.createResponseExceptionMapper(exType);
>             if (mapper != null) {
>                 return mapper;
>             }
>         }
> +        return pf.createResponseExceptionMapper(RuntimeException.class);
> -        return null; 
>     }
> {code} 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message