Skip to content

Instantly share code, notes, and snippets.

@danbev
Created January 21, 2013 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbev/4585554 to your computer and use it in GitHub Desktop.
Save danbev/4585554 to your computer and use it in GitHub Desktop.
Error Message in AeroGear Controller Demo

Missing Parameter in the Request

Route:

route()
      .from("/cars")
      .on(RequestMethod.GET)
      .produces(MediaType.JSON)
      .paged().offset()
      .to(Cars.class).findCarsBy(param("offset"), param("color"), param("limit", "10"));

Call:

curl -i --header "Accept: application/json" "http://controllerdemo-danbev.rhcloud.com/aerogear-controller-demo/cars?color=red&limit=5"

Response:

HTTP/1.1 400 Bad Request
Content-Type: application/json;charset=UTF-8
Content-Length: 77
Connection: close

{"error":"AG_CONTROLLER000010: Parameter: 'offset' was missing from Request"}

Invalid Paging Parameter name in Route configuration

Route:

route()
      .from("/cars")
      .on(RequestMethod.GET)
      .produces(MediaType.JSON)
      .paged().offset()
      .to(Cars.class).findCarsBy(param("offsset", "0"), param("color"), param("limit", "10"));

Notice that offset on the route was missspelled. Using paged().offset() is using the default values for the offset and limit parameter names.
These can be changed by using offsetParamName or limitParamName to avoid this error but is useful for troubleshooting during development.

Call:

curl -i --header "Accept: application/json" "http://controllerdemo-danbev.rhcloud.com/aerogear-controller-demo/cars?offset=0&color=red&limit=5"

Response:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json;charset=UTF-8
Content-Length: 174
Connection: close

{"error":"AG_CONTROLLER000014: The paging parameter 'offset' was not found in the request. The route is expecting the following arguments: '{offsset=0, color=red, limit=5}'"}

Inconsistency between paging parameter and route parameter

Route:

route()
      .from("/cars")
      .on(RequestMethod.GET)
      .produces(MediaType.JSON, CustomMediaTypeResponder.MEDIA_TYPE)
      .paged().offset("myoffset").limitParamName("mylimit")
      .to(Cars.class).findCarsBy(param("offset", "0"), param("color"), param("limit", "10"));

Call:

curl -i --header "Accept: application/json" "http://controllerdemo-danbev.rhcloud.com/aerogear-controller-demo/cars?offset=0&color=red&limit=5"

Response:

HTTP/1.1 400 Bad Request
Content-Type: application/json;charset=UTF-8
Content-Length: 82
Connection: close

{"error":"AG_CONTROLLER000015: Paging parameter 'myoffset' missing from request."}

Configuration of Error Response Messages and status codes

The error message and response status code can be controlled by the use of Error Routes. For example, the examples above use the following error routes:

route()
      .on(PagingRequestException.class)
      .produces(MediaType.JSON)
      .to(Error.class).handlePagingRequestException(param(PagingRequestException.class));
route()
      .on(PagingRouteException.class)
      .produces(MediaType.JSON)
      .to(Error.class).handlePagingRouteException(param(PagingRouteException.class));
route()
      .on(MissingRequestParameterException.class)
      .produces(MediaType.JSON)
      .to(Error.class).handleMissingRequestParameter(param(MissingRequestParameterException.class));

And the corresponding method implementations look like this:

public ErrorResponse handlePagingRequestException(final PagingRequestException e) {
    return new JsonErrorResponse(HttpServletResponse.SC_BAD_REQUEST)
        .message("error", e.getMessage());
}
    
public ErrorResponse handlePagingRouteException(final PagingRouteException e) {
    return new JsonErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
        .message("error", e.getMessage());
}
public ErrorResponse handleMissingRequestParameter(final MissingRequestParameterException e) {
    return new JsonErrorResponse(HttpServletResponse.SC_BAD_REQUEST)
        .message("error", e.getMessage());
}

You could choose to return a different error message, change the returned response status code as you see fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment