0

In my JAXRS app, I issue a LicenseKeyException when the license is invalid. That exception is trapped by an ExceptionMapper which responds to the caller by a 403 status code, with an entity holding some extra info which I want to send along the status.

But on my javascript client, in the XMLHttpRequest, I only get the status, not the response.

Java code

@Provider
public class LicenseKeyExceptionMapper implements ExceptionMapper<LicenseKeyException> {
    @Override
    public Response toResponse(final LicenseKeyException exception) {
        return Response
            .status(Response.Status.FORBIDDEN)
            .entity(exception.getMessage()) // I want this to be available to the javascript caller
            .build();
    }
}

Javascript code

let xhr = new XMLHttpRequest();
xhr.open(opt.method, makeActuelURL(opt.url), true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("licenseKey", opt.licenseKey);
let json = JSON.stringify({...opt, charsetName: "utf-8"});
xhr.responseType = "json";
xhr.onload = () => {
    if (xhr.status === 200)
        resolve(xhr);
    else
        reject(makeError(xhr));
};
xhr.onerror = () => reject(makeError(xhr));
xhr.send(json);

When the status is not 200 (I tried 500 and 403) the response field in the xhr object is null. I was expecting to find the entity I thought I put in there in the Java code.

What I am doing wrong ?

Sxilderik
  • 796
  • 6
  • 20

0 Answers0