private void updateResponseContextWithError(Throwable throwable, FilterResponseContext responseContext) { assert throwable != null; setErrorResponseHeader(responseContext); // Note: The original exception, if there were one, is replaced silently. Be careful in logging the original // exception, though, as it may cause excessive logging in existing applications. RestLiServiceException restLiServiceException = RestLiServiceException.fromThrowable(throwable); responseContext.getResponseData().getResponseEnvelope().setExceptionInternal(restLiServiceException); }
/** * Create a {@link FilterResponseContext} based on the given error. * * @param throwable Error obtained from the resource method invocation. * * @return {@link FilterResponseContext} corresponding to the given input. */ public FilterResponseContext fromThrowable(Throwable throwable) { Map<String, String> requestHeaders = _request.getHeaders(); Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, ProtocolVersionUtil.extractProtocolVersion(requestHeaders).toString()); headers.put(HeaderUtil.getErrorResponseHeaderName(requestHeaders), RestConstants.HEADER_VALUE_ERROR); final RestLiResponseData<?> responseData = _responseHandler.buildExceptionResponseData(_method, RestLiServiceException.fromThrowable(throwable), headers, Collections.emptyList()); return new FilterResponseContext() { @Override public RestLiResponseData<?> getResponseData() { return responseData; } }; } }
protected RestLiResponseException buildPreRoutingError(Throwable throwable, Request request) { Map<String, String> requestHeaders = request.getHeaders(); Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, ProtocolVersionUtil.extractProtocolVersion(requestHeaders).toString()); headers.put(HeaderUtil.getErrorResponseHeaderName(requestHeaders), RestConstants.HEADER_VALUE_ERROR); RestLiServiceException restLiServiceException = RestLiServiceException.fromThrowable(throwable); ErrorResponse errorResponse = _errorResponseBuilder.buildErrorResponse(restLiServiceException); RestLiResponse restLiResponse = new RestLiResponse.Builder() .status(restLiServiceException.getStatus()) .entity(errorResponse) .headers(headers) .cookies(Collections.emptyList()) .build(); return new RestLiResponseException(throwable, restLiResponse); }