nextFilter.onError(new RestException(restResponse, e), requestContext, wireAttrs);
/** * Factory method to obtain a new instance for a specified HTTP status code with the given cause. * * @param status the HTTP status code for the exception. * @param throwable the throwable to be used as the cause for this exception. * @return a new instance, as described above. */ public static RestException forError(int status, Throwable throwable) { return new RestException(RestStatus.responseForError(status, throwable), throwable); }
/** * Factory method to obtain a new instance for the specified HTTP status code. * * @param status the HTTP status code for the exception. * @param detail the detail message to be returned with this exception. * @return a new instance, as described above. */ public static RestException forError(int status, String detail) { return new RestException(RestStatus.responseForStatus(status, detail)); }
/** * Factory method to obtain a new instance for a specified HTTP status code with the given cause, and an option * to disable stack trace. Consider setting {@code writableStackTrace} to {@code false} to conserve computation * cost if the stacktrace does not contribute meaningful insights. * * @param status the HTTP status code for the exception. * @param message the exception message for this exception. * @param throwable the throwable to be used as the cause for this exception. * @return a new instance, as described above. * @param writableStackTrace the exception stacktrace is filled in if true; false otherwise. */ public static RestException forError(int status, String message, Throwable throwable, boolean writableStackTrace) { return new RestException(RestStatus.responseForError(status, throwable), message, throwable, writableStackTrace); }
@Override public void onSuccess(RestResponse result) { callback.onSuccess(new RestException(result, streamException.getMessage(), streamException.getCause())); } }, addContentLengthHeader);
@Override public Greeting call() throws Exception { throw new RestException(new RestResponseBuilder().setStatus(401).build()); } });
private boolean replayResponse(RestRequest req, RequestContext requestContext, NextFilter<RestRequest, RestResponse> nextFilter) { final RestResponse res = _db.replay(req); if (res != null) { _log.debug("Using cached response for request: " + req.getURI()); // We create an empty map instead of Collections.emptyMap, because upstream filters may // try to modify the map. final Map<String, String> wireAttrs = new HashMap<String, String>(); // For symmetry with CaptureFilter - if the REST response is "not OK" then we treat it as an // exception. if (!RestStatus.isOK(res.getStatus())) { nextFilter.onError(new RestException(res), requestContext, wireAttrs); } else { nextFilter.onResponse(res, requestContext, wireAttrs); } return true; } return false; } }
@Action(name = "promise") public Promise<Greeting> promise(@ActionParam(value = "id") final String key) { if (key.equals("returnNonService")) { // Non-RestLiServiceException passed to callback return Promises.error(new RestException(new RestResponseBuilder().setStatus(401).build())); } // RestLiServiceException passed to callback if (key.equals("returnService")) { return Promises.error(new RestLiServiceException(HttpStatus.S_401_UNAUTHORIZED)); } //RestLiServiceException thrown if (key.equals("throwService")) { throw new RestLiServiceException(HttpStatus.S_401_UNAUTHORIZED); } if (key.equals("throwNonService")) { // Non-RestLiServiceException thrown throw new IllegalStateException(); } return Promises.value(new Greeting()); }
@Action(name = "callback") public void callback(@ActionParam(value = "id") final String key, @CallbackParam final Callback<Greeting> callback) { // Non-RestLiServiceException passed to callback if (key.equals("returnNonService")) { callback.onError(new RestException(new RestResponseBuilder().setStatus(401).build())); } // RestLiServiceException passed to callback if (key.equals("returnService")) { callback.onError(new RestLiServiceException(HttpStatus.S_401_UNAUTHORIZED)); } //RestLiServiceException thrown if (key.equals("throwService")) { throw new RestLiServiceException(HttpStatus.S_401_UNAUTHORIZED); } if (key.equals("throwNonService")) { // Non-RestLiServiceException thrown throw new IllegalStateException(); } callback.onSuccess(new Greeting()); } }
RestException exception = new RestException(individualRestResponse, "Received error " + individualRestResponse.getStatus()); callback.onError(exception);
nextFilter.onError(new RestException(restResponse, e), requestContext, wireAttrs);
public static RestException buildRestException(RestLiResponseException restLiResponseException) { RestLiResponse restLiResponse = restLiResponseException.getRestLiResponse(); RestResponseBuilder responseBuilder = new RestResponseBuilder() .setHeaders(restLiResponse.getHeaders()) .setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())) .setStatus(restLiResponse.getStatus().getCode()); if (restLiResponse.hasData()) { DataMap dataMap = restLiResponse.getDataMap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); DataMapUtils.write(dataMap, null, baos, true); responseBuilder.setEntity(ByteString.unsafeWrap(baos.toByteArray())); // TODO: Error response not always built in requested content type. responseBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, ContentType.JSON.getHeaderKey()); } RestResponse restResponse = responseBuilder.build(); return new RestException(restResponse, restLiResponseException.getCause()); }
@Override public void onResponse(TransportResponse<RestResponse> response) { if (response.hasError()) { response = TransportResponseImpl.error(new RemoteInvocationException("Failed to get response from server for URI " + uri, response.getError()), response.getWireAttributes()); } else if (!RestStatus.isOK(response.getResponse().getStatus())) { response = TransportResponseImpl.error(new RestException(response.getResponse(), "Received error " + response.getResponse() .getStatus() + " from server for URI " + uri), response.getWireAttributes()); } callback.onResponse(response); } };