@Override
    public Response toResponse(Throwable t) {

        // Pass WebApplicationException responses through untouched
        if (t instanceof WebApplicationException)
            return ((WebApplicationException) t).getResponse();
        
        // Ensure any associated session is invalidated if unauthorized 
        if (t instanceof GuacamoleUnauthorizedException) {
            String token = authenticationService.getAuthenticationToken(request);
            if (authenticationService.destroyGuacamoleSession(token))
                logger.debug("Implicitly invalidated session for token \"{}\"", token);
        }
        
        // Translate GuacamoleException subclasses to HTTP error codes 
        if (t instanceof GuacamoleException) {

            // Always log the human-readable details of GuacacamoleExceptions
            // for the benefit of the administrator
            if (t instanceof GuacamoleClientException)
                logger.debug("Client request rejected: {}", t.getMessage());
            else {
                logger.error("Request could not be processed: {}", t.getMessage());
                logger.debug("Processing of request aborted by extension.", t);
            }

            return Response
                    .status(((GuacamoleException) t).getHttpStatusCode())
                    .entity(new APIError((GuacamoleException) t))
                    .type(MediaType.APPLICATION_JSON)
                    .build();
        }
        
        // Wrap unchecked exceptions
        String message = t.getMessage();
        if (message != null)
            logger.error("Unexpected internal error: {}", message);
        else
            logger.error("An internal error occurred, but did not contain "
                    + "an error message. Enable debug-level logging for "
                    + "details.");
            
        logger.debug("Unexpected error in REST endpoint.", t);
            
        return Response
                .status(Response.Status.INTERNAL_SERVER_ERROR)
                .entity(new APIError(
                        new GuacamoleException("Unexpected internal error", t)))
                .type(MediaType.APPLICATION_JSON)
                .build();
        
    }
The authentication service associated with the currently active session.
guacamole/src/main/java/org/apache/guacamole/rest/RESTExceptionMapper.java