If the error occurs in your own method you can use a Response as return type for your methods. Then you can specify both the status code and the returned message body.
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("<your message here>").build());
In case the error occurs in some other framework, the exception should be logged to stderr. Depending on what Webserver you use you might be able to configure it in order to send exceptions as Response bodies.