Search in sources :

Example 1 with ErrorResponse

use of org.alfresco.rest.framework.core.exceptions.ErrorResponse in project alfresco-remote-api by Alfresco.

the class ExceptionResolverTests method testWebscriptException.

@Test
public void testWebscriptException() {
    ErrorResponse response = assistant.resolveException(new WebScriptException(null));
    assertNotNull(response);
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new WebScriptException(HttpServletResponse.SC_UNAUTHORIZED, "Authentication failed for Web Script "));
    assertNotNull(response);
    // default to INTERNAL_SERVER_ERROR
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusCode());
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ErrorResponse(org.alfresco.rest.framework.core.exceptions.ErrorResponse) Test(org.junit.Test)

Example 2 with ErrorResponse

use of org.alfresco.rest.framework.core.exceptions.ErrorResponse in project alfresco-remote-api by Alfresco.

the class ResponseWriter method renderErrorResponse.

/**
 * Renders a JSON error response
 *
 * @param errorResponse The error
 * @param res           web script response
 * @throws IOException
 */
default void renderErrorResponse(final ErrorResponse errorResponse, final WebScriptResponse res, final JacksonHelper jsonHelper) throws IOException {
    String logId = "";
    if (Status.STATUS_INTERNAL_SERVER_ERROR == errorResponse.getStatusCode() || resWriterLogger().isDebugEnabled()) {
        logId = org.alfresco.util.GUID.generate();
        resWriterLogger().error(logId + " : " + errorResponse.getStackTrace());
    }
    String stackMessage = I18NUtil.getMessage(DefaultExceptionResolver.STACK_MESSAGE_ID);
    final ErrorResponse errorToWrite = new ErrorResponse(errorResponse.getErrorKey(), errorResponse.getStatusCode(), errorResponse.getBriefSummary(), stackMessage, logId, errorResponse.getAdditionalState(), DefaultExceptionResolver.ERROR_URL);
    setContentInfoOnResponse(res, DEFAULT_JSON_CONTENT);
    // Status must be set before the response is written by Jackson (which will by default close and commit the response).
    // In a r/w txn, web script buffered responses ensure that it doesn't really matter but for r/o txns this is important.
    res.setStatus(errorToWrite.getStatusCode());
    jsonHelper.withWriter(res.getOutputStream(), new JacksonHelper.Writer() {

        @SuppressWarnings("unchecked")
        @Override
        public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
            JSONObject obj = new JSONObject();
            obj.put("error", errorToWrite);
            objectMapper.writeValue(generator, obj);
        }
    });
}
Also used : JSONObject(org.json.simple.JSONObject) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JacksonHelper(org.alfresco.rest.framework.jacksonextensions.JacksonHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ErrorResponse(org.alfresco.rest.framework.core.exceptions.ErrorResponse)

Example 3 with ErrorResponse

use of org.alfresco.rest.framework.core.exceptions.ErrorResponse in project alfresco-remote-api by Alfresco.

the class ExceptionResolverTests method testMatchException.

// 04180006 Authentication failed for Web Script org/alfresco/api/ResourceWebScript.get
@Test
public void testMatchException() {
    ErrorResponse response = assistant.resolveException(new ApiException(null));
    assertNotNull(response);
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new InvalidArgumentException(null));
    // default to STATUS_BAD_REQUEST
    assertEquals(400, response.getStatusCode());
    response = assistant.resolveException(new InvalidQueryException(null));
    // default to STATUS_BAD_REQUEST
    assertEquals(400, response.getStatusCode());
    response = assistant.resolveException(new NotFoundException(null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new EntityNotFoundException(null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new RelationshipResourceNotFoundException(null, null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new PermissionDeniedException(null));
    // default to STATUS_FORBIDDEN
    assertEquals(403, response.getStatusCode());
    response = assistant.resolveException(new UnsupportedResourceOperationException(null));
    // default to STATUS_METHOD_NOT_ALLOWED
    assertEquals(405, response.getStatusCode());
    response = assistant.resolveException(new DeletedResourceException(null));
    // default to STATUS_METHOD_NOT_ALLOWED
    assertEquals(405, response.getStatusCode());
    response = assistant.resolveException(new ConstraintViolatedException(null));
    // default to STATUS_CONFLICT
    assertEquals(409, response.getStatusCode());
    response = assistant.resolveException(new StaleEntityException(null));
    // default to STATUS_CONFLICT
    assertEquals(409, response.getStatusCode());
    // Try a random exception
    response = assistant.resolveException(new FormNotFoundException(null));
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new InsufficientStorageException(null));
    assertEquals(507, response.getStatusCode());
    response = assistant.resolveException(new IntegrityException(null));
    assertEquals(422, response.getStatusCode());
}
Also used : UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) FormNotFoundException(org.alfresco.repo.forms.FormNotFoundException) RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) DeletedResourceException(org.alfresco.rest.framework.core.exceptions.DeletedResourceException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ErrorResponse(org.alfresco.rest.framework.core.exceptions.ErrorResponse) RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FormNotFoundException(org.alfresco.repo.forms.FormNotFoundException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) StaleEntityException(org.alfresco.rest.framework.core.exceptions.StaleEntityException) InvalidQueryException(org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) Test(org.junit.Test)

Aggregations

ErrorResponse (org.alfresco.rest.framework.core.exceptions.ErrorResponse)3 Test (org.junit.Test)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 FormNotFoundException (org.alfresco.repo.forms.FormNotFoundException)1 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)1 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 DeletedResourceException (org.alfresco.rest.framework.core.exceptions.DeletedResourceException)1 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)1 InsufficientStorageException (org.alfresco.rest.framework.core.exceptions.InsufficientStorageException)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)1 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)1 RelationshipResourceNotFoundException (org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException)1 StaleEntityException (org.alfresco.rest.framework.core.exceptions.StaleEntityException)1 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)1