Search in sources :

Example 66 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class ErrorResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object object, Map<String, String> headers, List<HttpCookie> cookies) {
    RestLiServiceException exceptionResult = (RestLiServiceException) object;
    if (_errorResponseFormat.showHeaders()) {
        final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(headers);
        headers.put(HeaderUtil.getErrorResponseHeaderName(protocolVersion), RestConstants.HEADER_VALUE_ERROR);
    }
    final ResourceMethod type;
    if (routingResult != null && routingResult.getResourceMethod() != null) {
        type = routingResult.getResourceMethod().getMethodType();
    } else {
        RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(exceptionResult, headers, cookies);
        return responseData;
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(exceptionResult, headers, cookies);
    responseData.setResponseEnvelope(EnvelopeBuilderUtil.buildBlankResponseEnvelope(type, responseData));
    return responseData;
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 67 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class AlbumResource method update.

// update an existing photo with given entity
@Override
public UpdateResponse update(Long key, Album entity) {
    final Album currPhoto = _db.getData().get(key);
    if (currPhoto == null) {
        return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
    }
    // disallow changing entity ID and URN
    if (entity.hasId() || entity.hasUrn()) {
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Album ID is not acceptable in request");
    }
    // make sure the ID in the entity is consistent with the key in the database
    entity.setId(key);
    entity.setUrn(String.valueOf(key));
    _db.getData().put(key, entity);
    return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Album(com.linkedin.restli.example.Album)

Example 68 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class PhotoResource method update.

// update an existing photo with given entity
@Override
public UpdateResponse update(Long key, Photo entity) {
    final Photo currPhoto = _db.getData().get(key);
    if (currPhoto == null) {
        return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
    }
    //ID and URN are required fields, so use a dummy value to denote "empty" fields
    if ((entity.hasId() && entity.getId() != -1) || (entity.hasUrn() && !entity.getUrn().equals(""))) {
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Photo ID is not acceptable in request");
    }
    // make sure the ID in the entity is consistent with the key in the database
    entity.setId(key);
    entity.setUrn(String.valueOf(key));
    _db.getData().put(key, entity);
    return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Photo(com.linkedin.restli.example.Photo)

Example 69 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class PhotoResource method batchGet.

@Override
public BatchResult<Long, Photo> batchGet(Set<Long> ids) {
    Map<Long, Photo> result = new HashMap<Long, Photo>();
    Map<Long, RestLiServiceException> errors = new HashMap<Long, RestLiServiceException>();
    for (Long key : ids) {
        if (get(key) != null) {
            result.put(key, get(key));
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "No photo with id=" + key + " has been found."));
        }
    }
    return new BatchResult<Long, Photo>(result, errors);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) Photo(com.linkedin.restli.example.Photo) BatchResult(com.linkedin.restli.server.BatchResult)

Example 70 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestRestLiResponseHandler method testRestErrors.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
void testRestErrors(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestResponse response;
    RestLiServiceException ex;
    final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
    // #1
    ex = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "missing fields");
    response = _responseHandler.buildResponse(request, buildRoutingResult(request, acceptTypeData.acceptHeaders), ex);
    checkResponse(response, 400, 3, acceptTypeData.responseContentType, ErrorResponse.class.getName(), null, true, true, errorResponseHeaderName);
    DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
    assertEquals(dataMap.getInteger("status"), Integer.valueOf(400));
    assertEquals(dataMap.getString("message"), "missing fields");
    // #2
    ex = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "missing fields").setServiceErrorCode(11);
    response = _responseHandler.buildResponse(request, buildRoutingResult(request, acceptTypeData.acceptHeaders), ex);
    checkResponse(response, 400, 3, acceptTypeData.responseContentType, ErrorResponse.class.getName(), null, true, true, errorResponseHeaderName);
    dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
    assertEquals(dataMap.getInteger("status"), Integer.valueOf(400));
    assertEquals(dataMap.getString("message"), "missing fields");
    assertEquals(dataMap.getInteger("serviceErrorCode"), Integer.valueOf(11));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)93 Test (org.testng.annotations.Test)36 HashMap (java.util.HashMap)31 UpdateResponse (com.linkedin.restli.server.UpdateResponse)18 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)17 RoutingException (com.linkedin.restli.server.RoutingException)17 RestException (com.linkedin.r2.message.rest.RestException)16 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)16 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)16 BeforeTest (org.testng.annotations.BeforeTest)16 DataMap (com.linkedin.data.DataMap)14 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)13 Map (java.util.Map)13 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)12 EmptyRecord (com.linkedin.restli.common.EmptyRecord)12 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)12 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)11 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)11 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)11 RecordTemplate (com.linkedin.data.template.RecordTemplate)10