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());
}
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);
}
});
}
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());
}
Aggregations