Saturday, 28 May 2011

sMash: REST handling errors

Two examples showing how to handle errors with sMash REST development where the first example sets response status to HTTP_NOT_FOUND (404) while the 2nd method sets response status to HTTP_INTERNAL_ERROR.
Take into consideration that in normal REST development, there isn't need to set the view to "error" and put an error message as the status is enough, but this makes troubleshooting problems using a browser much easier during initial REST development phase.

def setStatusNotFound(id){
logger.FINER {"Start"};
//Construct error
request.view="error";
request.status=HttpURLConnection.HTTP_NOT_FOUND;
request.header.out."Content-Type"= "text/html";
request.error.message="Object with ID ${id} is not found";
render();
logger.FINER {"End"};
}

def setInternalServerError(ex){
logger.INFO {"Start"};
//Construct error
request.view="error";
request.status=HttpURLConnection.HTTP_INTERNAL_ERROR;
request.header.out."Content-Type"= "text/html";
request.error.message="Application internal error\n";
request.error.message+=ex.message;
//request.error.exception="test";
render();
logger.FINER {"End"};
}

No comments:

Post a Comment