use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class TailoredPeersClusterMap method doBadArgsTest.
// badArgsTest() helpers.
/**
* Does the test where bad args are provided in the request to {@link GetPeersHandler}.
* @param name the name of the host whose peers are required. Can be {@code null} if this param should be omitted.
* @param port the port of the host whose peers are required. Can be {@code null} if this param should be omitted.
* @param expectedErrorCode the {@link RestServiceErrorCode} expected in response.
* @throws Exception
*/
private void doBadArgsTest(String name, String port, RestServiceErrorCode expectedErrorCode) throws Exception {
StringBuilder uri = new StringBuilder(Operations.GET_PEERS + "?");
if (name != null) {
uri.append(GetPeersHandler.NAME_QUERY_PARAM).append("=").append(name);
}
if (port != null) {
if (name != null) {
uri.append("&");
}
uri.append(GetPeersHandler.PORT_QUERY_PARAM).append("=").append(port);
}
JSONObject data = new JSONObject();
data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET);
data.put(MockRestRequest.URI_KEY, uri);
RestRequest restRequest = new MockRestRequest(data, null);
try {
sendRequestGetResponse(restRequest, new MockRestResponseChannel());
fail("Request should have failed");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", expectedErrorCode, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class GetReplicasHandlerTest method getReplicasWithBadInputTest.
/**
* Tests reactions of the {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)} operation to bad input -
* specifically if we do not include required parameters.
* @throws Exception
*/
@Test
public void getReplicasWithBadInputTest() throws Exception {
// bad input - invalid blob id.
try {
getReplicasHandler.getReplicas("12345", new MockRestResponseChannel());
fail("Exception should have been thrown because the blobid is invalid");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.NotFound, e.getErrorCode());
}
// bad input - invalid blob id for this cluster map.
String blobId = "AAEAAQAAAAAAAADFAAAAJDMyYWZiOTJmLTBkNDYtNDQyNS1iYzU0LWEwMWQ1Yzg3OTJkZQ.gif";
try {
getReplicasHandler.getReplicas(blobId, new MockRestResponseChannel());
fail("Exception should have been thrown because the blobid is invalid");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.NotFound, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class GetSignedUrlHandlerTest method urlSigningServiceFailureTest.
/**
* Tests where {@link UrlSigningService} fails.
* @throws Exception
*/
@Test
public void urlSigningServiceFailureTest() throws Exception {
String msg = "@@expected";
urlSigningServiceFactory.getSignedUrlException = new RestServiceException(msg, RestServiceErrorCode.InternalServerError);
verifyFailureWithMsg(msg);
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class AccountAndContainerInjector method injectAccountAndContainerForPostRequest.
/**
* Injects target {@link Account} and {@link Container} for PUT requests. This method also ensures required headers
* are present for the PUT requests that use serviceId as the account name, and the PUT requests that carry both the
* {@code x-ambry-target-account} and {@code x-ambry-target-container} headers.
* @param restRequest The Put {@link RestRequest}.
* @throws RestServiceException
*/
public void injectAccountAndContainerForPostRequest(RestRequest restRequest) throws RestServiceException {
accountAndContainerSanityCheck(restRequest);
if (getHeader(restRequest.getArgs(), Headers.TARGET_ACCOUNT_NAME, false) != null || getHeader(restRequest.getArgs(), Headers.TARGET_CONTAINER_NAME, false) != null) {
ensureRequiredHeadersOrThrow(restRequest, requiredAmbryHeadersForPutWithAccountAndContainerName);
frontendMetrics.putWithAccountAndContainerHeaderRate.mark();
injectAccountAndContainerUsingAccountAndContainerHeaders(restRequest);
} else if (frontendConfig.frontendAllowServiceIdBasedPostRequest) {
ensureRequiredHeadersOrThrow(restRequest, requiredAmbryHeadersForPutWithServiceId);
frontendMetrics.putWithServiceIdForAccountNameRate.mark();
String serviceId = getHeader(restRequest.getArgs(), Headers.SERVICE_ID, true);
boolean isPrivate = isPrivate(restRequest.getArgs());
injectAccountAndContainerUsingServiceId(restRequest, serviceId, isPrivate);
} else {
throw new RestServiceException("Missing either " + Headers.TARGET_ACCOUNT_NAME + " or " + Headers.TARGET_CONTAINER_NAME + " header", RestServiceErrorCode.BadRequest);
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class AmbryBlobStorageService method submitResponse.
/**
* Submits the response and {@code responseBody} (and any {@code exception})for the {@code restRequest} to the
* {@code responseHandler}.
* @param restRequest the {@link RestRequest} for which a response is ready.
* @param restResponseChannel the {@link RestResponseChannel} over which the response can be sent.
* @param responseBody the body of the response in the form of a {@link ReadableStreamChannel}.
* @param exception any {@link Exception} that occurred during the handling of {@code restRequest}.
*/
void submitResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel responseBody, Exception exception) {
try {
if (exception != null && exception instanceof RouterException) {
exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode()));
}
responseHandler.handleResponse(restRequest, restResponseChannel, responseBody, exception);
} catch (Exception e) {
frontendMetrics.responseSubmissionError.inc();
if (exception != null) {
logger.error("Error submitting response to response handler", e);
} else {
exception = e;
}
logger.error("Handling of request {} failed", restRequest.getUri(), exception);
restResponseChannel.onResponseComplete(exception);
if (responseBody != null) {
try {
responseBody.close();
} catch (IOException ioe) {
frontendMetrics.resourceReleaseError.inc();
logger.error("Error closing ReadableStreamChannel", e);
}
}
}
}
Aggregations