Customize the error messages

Hello
I am developing a REST webservice and I have to customize the error messages for NotAuthorizedException, NotAcceptableException, BadRequestException

For example, for the BadRequestException, I tried :

if( !this.ipgPattern.matcher(ipg).matches() )
    throw new BadRequestException("Incorrect IPG number: 11 numeric characters expected");

or

if( !this.ipgPattern.matcher(ipg).matches() )
    throw new WebApplicationException("Incorrect IPG number: 11 numeric characters expected", 400);

Nothing happens, I still have on my screen the default error message :
image

Does anyone have an idea to change message or description ?

Thanks
Thierry

I managed to produce my own error pages :smiling_face_with_tear:

image

… and this is how :

  • Firstly, I created a MyPayaraException class that uses the same html construction as the error pages generated by Payara
package fr.inrae.theirman.exceptions;

public class MyPayaraException
{
    public String message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Payara Server 5.192 #badassfish - Error report</title><style type=\"text/css\"><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status {{HTTP_STATUS_CODE}} - {{HTTP_STATUS_EXCEPTION}}</i></h1><hr/><p><b>Code :</b> {{HTTP_STATUS_CODE}}</p><p><b>Message :</b> {{HTTP_STATUS_EXCEPTION}}</p><p><b>Description :</b> {{HTTP_STATUS_MESSAGE}}</p><hr/><h3>Payara Server 5.192 #badassfish</h3></body></html>";
    
    public MyPayaraException(String httpCode, String message)
    {
        this.message = this.message.replace("{{HTTP_STATUS_CODE}}", httpCode)
                                   .replace("{{HTTP_STATUS_EXCEPTION}}", getExceptionByCode(httpCode))
                                   .replace("{{HTTP_STATUS_MESSAGE}}", message);
    }

    private String getExceptionByCode(String code)
    {
        switch(code)
        {
            case "400":     return "BadRequestException";
            case "401":     return "NotAuthorizedException";
            case "403":     return "ForbiddenException";
            case "404":     return "NotFoundException";
            case "405":     return "NotAllowedException";
            case "406":     return "NotAcceptableException";
            case "415":     return "NotSupportedException";
            case "500":     return "InternalServerErrorException";
            case "503":     return "ServiceUnavailableException";
            default:        return "CustomException";
        }        
    }
    
    public String toString()
    {
        return this.message;
    }

}
  • Secondly, I use this class to return an html code instead of throwing an exception
        if( !this.ipgPattern.matcher(ipg).matches() )
            return new MyPayaraException("400", "Incorrect IPG number <i>(11 numeric characters expected)</i>").toString();

This may not be the best approach to do it, certainly Payara has foreseen this use case! but as it is not documented, I had to do it in a hurry :slight_smile:

Hi @theirman,

That certainly is one way of doing it! There are multiple different ways to approach this, so I will share a few.


Method 1

You could create your own error pages and then using the web.xml deployment descriptor within your application, redirect exceptions to the error page you would like.

With Jersey 2, you can map error codes or exception types to error pages by selecting one of the following:

<error-page>
   <error-code>403</error-code>
   <location>/path/to/errorpage</location>
</error-page>
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/path/to/errorpage</location>
</error-page>

Method 2

Jersey also has an Exception Mapper which would allow you to have a custom error page in a more similar manner to the way you have explained above. This method also doesn’t require you to make an html or jsp error page, unlike the other two methods, which you may prefer.

There is an excellent guide for this on howtodoinjava which I would recommend you read/follow if you wish to use this method as it contains a fully working example which would be too big to include here! That page can be found here Jersey exception handling - Jersey ExceptionMapper Example


Method 3

For a global configuration, you would add a system property named send-error_X (where X is a number, e.g. send-error_1) for every error page you want to override.

The value of the property specifies the error code, path to the html page relative to the ${com.sun.aas.instanceRoot} variable, and a reason for the error (e.g. Resource not found).

If you specify more custom error pages (for different error codes), you would choose a different number in the system property name, e.g. send-error_1, send-error_2, etc.

An example would be:
send-error_1=code=404 path=${com.sun.aas.instanceRoot}/docroot/404.html reason=Resource_not_found

send-error_1=code=403 path=${com.sun.aas.instanceRoot}/docroot/404.html reason=Forbidden

Additional documentation for this method can be found here: Virtual Servers :: Payara Community Documentation


That’s quite a lot of information but hopefully it allows you to pick the method of providing a custom error page you like best!

Thanks,
James

1 Like