use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdCommentsPost.
/**
* Update a comment
*
* @param apiId API ID
* @param body comment body
* @param request msf4j request object
* @return comment update response
* @throws NotFoundException if this method is not defined in ApisApiServiceImpl
*/
@Override
public Response apisApiIdCommentsPost(String apiId, CommentDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
Comment comment = CommentMappingUtil.fromDTOToComment(body, username);
String createdCommentId = apiStore.addComment(comment, apiId);
Comment createdComment = apiStore.getCommentByUUID(createdCommentId, apiId);
CommentDTO createdCommentDTO = CommentMappingUtil.fromCommentToDTO(createdComment);
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_COMMENTS + "/" + createdCommentId);
String fingerprint = getEtag(comment.getUuid(), request.getProperty("LOGGED_IN_USER").toString());
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).header(HttpHeaders.ETAG, "\"" + fingerprint + "\"").entity(createdCommentDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding comment to api : " + apiId;
Map<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiId());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for comment";
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.
the class GlobalThrowableMapper method toResponse.
@Override
public Response toResponse(Throwable e) {
if (e instanceof ClientErrorException) {
log.error("Client error", e);
return ((ClientErrorException) e).getResponse();
}
if (e instanceof NotFoundException) {
log.error("Resource not found", e);
return ((NotFoundException) e).getResponse();
}
if (e instanceof PreconditionFailedException) {
log.error("Precondition failed", e);
return ((PreconditionFailedException) e).getResponse();
}
if (e instanceof BadRequestException) {
log.error("Bad request", e);
return ((BadRequestException) e).getResponse();
}
if (e instanceof ConstraintViolationException) {
log.error("Constraint violation", e);
return ((ConstraintViolationException) e).getResponse();
}
if (e instanceof ForbiddenException) {
log.error("Resource forbidden", e);
return ((ForbiddenException) e).getResponse();
}
if (e instanceof ConflictException) {
log.error("Conflict", e);
return ((ConflictException) e).getResponse();
}
if (e instanceof MethodNotAllowedException) {
log.error("Method not allowed", e);
return ((MethodNotAllowedException) e).getResponse();
}
if (e instanceof InternalServerErrorException) {
String errorMessage = "The server encountered an internal error : " + e.getMessage();
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON_TYPE).entity(e500).build();
}
if (e instanceof JsonParseException) {
String errorMessage = "Malformed request body.";
log.error(errorMessage, e);
// noinspection ThrowableResultOfMethodCallIgnored
return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
}
if (e instanceof JsonMappingException) {
if (e instanceof UnrecognizedPropertyException) {
UnrecognizedPropertyException unrecognizedPropertyException = (UnrecognizedPropertyException) e;
String unrecognizedProperty = unrecognizedPropertyException.getPropertyName();
String errorMessage = "Unrecognized property '" + unrecognizedProperty + "'";
log.error(errorMessage, e);
// noinspection ThrowableResultOfMethodCallIgnored
return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
} else {
String errorMessage = "One or more request body parameters contain disallowed values.";
log.error(errorMessage, e);
// noinspection ThrowableResultOfMethodCallIgnored
return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
}
}
if (e instanceof AuthenticationException) {
ErrorDTO errorDetail = new ErrorDTO();
errorDetail.setCode((long) 401);
errorDetail.setMoreInfo("");
errorDetail.setMessage("");
errorDetail.setDescription(e.getMessage());
return Response.status(Response.Status.UNAUTHORIZED).type(MediaType.APPLICATION_JSON_TYPE).entity(errorDetail).build();
}
// This occurs when received an empty body in an occasion where the body is mandatory
if (e instanceof EOFException) {
String errorMessage = "Request payload cannot be empty.";
log.error(errorMessage, e);
// noinspection ThrowableResultOfMethodCallIgnored
return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
}
if (e instanceof APIManagementException) {
ErrorHandler selectedErrorHandler = null;
List<Throwable> throwableList = ExceptionUtils.getThrowableList(e);
for (Throwable t : throwableList) {
if (t instanceof APIManagementException) {
APIManagementException apimException = (APIManagementException) t;
ErrorHandler errorHandler = apimException.getErrorHandler();
if (errorHandler != null) {
if (selectedErrorHandler == null) {
selectedErrorHandler = errorHandler;
} else {
selectedErrorHandler = errorHandler.getHttpStatusCode() < selectedErrorHandler.getHttpStatusCode() && errorHandler.getHttpStatusCode() > 0 ? errorHandler : selectedErrorHandler;
}
}
}
}
if (selectedErrorHandler != null) {
// logs the error as the error may be not logged by the origin
if (selectedErrorHandler.printStackTrace()) {
log.error("A defined exception has been captured and mapped to an HTTP response " + "by the global exception mapper ", e);
} else {
// Not to log the stack trace due to error code was mark as not print stacktrace.
log.error(e.getMessage());
if (log.isDebugEnabled()) {
log.debug("A defined exception has been captured and mapped to an HTTP response " + "by the global exception mapper ", e);
}
}
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(selectedErrorHandler);
return Response.status(Response.Status.fromStatusCode(selectedErrorHandler.getHttpStatusCode())).type(MediaType.APPLICATION_JSON_TYPE).entity(errorDTO).build();
}
}
// unknown exception log and return
log.error("An unknown exception has been captured by the global exception mapper.", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON_TYPE).entity(e500).build();
}
use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.
the class APIMappingUtil method getErrorListItemsDTOsFromErrorHandlers.
public static List<ErrorListItemDTO> getErrorListItemsDTOsFromErrorHandlers(List<ErrorHandler> errorHandlers) {
List<ErrorListItemDTO> errorListItemDTOs = new ArrayList<>();
for (ErrorHandler handler : errorHandlers) {
ErrorListItemDTO dto = new ErrorListItemDTO();
dto.setCode(handler.getErrorCode() + "");
dto.setMessage(handler.getErrorMessage());
dto.setDescription(handler.getErrorDescription());
errorListItemDTOs.add(dto);
}
return errorListItemDTOs;
}
use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.
the class RestApiUtil method getErrorDTO.
/**
* Returns a generic errorDTO from a list of Error Handlers
*
* @param errorHandlers A List of error handler objects containing the error information
* @return A generic errorDTO with the specified details
*/
public static ErrorDTO getErrorDTO(List<ErrorHandler> errorHandlers) {
ErrorDTO errorDTO = new ErrorDTO();
for (int i = 0; i < errorHandlers.size(); i++) {
if (i == 0) {
ErrorHandler elementAt0 = errorHandlers.get(0);
errorDTO.setCode(elementAt0.getErrorCode());
errorDTO.setMoreInfo("");
errorDTO.setMessage(elementAt0.getErrorMessage());
errorDTO.setDescription(elementAt0.getErrorDescription());
} else {
ErrorListItemDTO errorListItemDTO = new ErrorListItemDTO();
errorListItemDTO.setCode(errorHandlers.get(i).getErrorCode() + "");
errorListItemDTO.setMessage(errorHandlers.get(i).getErrorMessage());
errorListItemDTO.setDescription(errorHandlers.get(i).getErrorDescription());
errorDTO.getError().add(errorListItemDTO);
}
}
return errorDTO;
}
use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.
the class RestApiUtil method getErrorDTO.
/**
* Returns a generic errorDTO from an Error Handler
*
* @param errorHandler ErrorHandler object containing the error information
* @return A generic errorDTO with the specified details
*/
public static ErrorDTO getErrorDTO(ErrorHandler errorHandler) {
ErrorDTO errorDTO = new ErrorDTO();
errorDTO.setCode(errorHandler.getErrorCode());
errorDTO.setMoreInfo("");
errorDTO.setMessage(errorHandler.getErrorMessage());
errorDTO.setDescription(errorHandler.getErrorDescription());
return errorDTO;
}
Aggregations