use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.
the class ErrorHandler method handle.
@Override
public void handle(RoutingContext routingContext) {
if (routingContext.failed()) {
Throwable throwable = routingContext.failure();
// management exception (resource not found, server error, ...)
if (throwable instanceof AbstractManagementException) {
AbstractManagementException technicalManagementException = (AbstractManagementException) throwable;
handleException(routingContext, technicalManagementException.getHttpStatusCode(), technicalManagementException.getMessage());
// oauth2 exception (token invalid exception)
} else if (throwable instanceof OAuth2Exception) {
OAuth2Exception oAuth2Exception = (OAuth2Exception) throwable;
handleException(routingContext, oAuth2Exception.getHttpStatusCode(), oAuth2Exception.getMessage());
} else if (throwable instanceof PolicyChainException) {
PolicyChainException policyChainException = (PolicyChainException) throwable;
handleException(routingContext, policyChainException.statusCode(), policyChainException.key() + " : " + policyChainException.getMessage());
} else if (throwable instanceof HttpException) {
HttpException httpStatusException = (HttpException) throwable;
handleException(routingContext, httpStatusException.getStatusCode(), httpStatusException.getPayload());
} else {
logger.error(throwable.getMessage(), throwable);
if (routingContext.statusCode() != -1) {
routingContext.response().setStatusCode(routingContext.statusCode()).end();
} else {
routingContext.response().setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end();
}
}
}
}
use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.
the class ErrorHandler method handle.
@Override
public void handle(RoutingContext routingContext) {
if (routingContext.failed()) {
Throwable throwable = routingContext.failure();
// management exception (resource not found, server error, ...)
if (throwable instanceof AbstractManagementException) {
AbstractManagementException technicalManagementException = (AbstractManagementException) throwable;
handleException(routingContext, "technical_error", technicalManagementException.getMessage());
// oauth2 exception (token invalid exception)
} else if (throwable instanceof OAuth2Exception) {
OAuth2Exception oAuth2Exception = (OAuth2Exception) throwable;
handleException(routingContext, oAuth2Exception.getOAuth2ErrorCode(), oAuth2Exception.getMessage());
} else if (throwable instanceof PolicyChainException) {
PolicyChainException policyChainException = (PolicyChainException) throwable;
handleException(routingContext, policyChainException.key(), policyChainException.getMessage());
} else if (throwable instanceof HttpException) {
HttpException httpStatusException = (HttpException) throwable;
handleException(routingContext, httpStatusException.getMessage(), httpStatusException.getPayload());
} else {
logger.error("An exception occurs while handling incoming request", throwable);
if (routingContext.statusCode() != -1) {
routingContext.response().setStatusCode(routingContext.statusCode()).end();
} else {
routingContext.response().setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end();
}
}
}
}
use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.
the class ServiceResourceServiceImpl method create.
@Override
public Single<ServiceResource> create(String domain, NewServiceResource newServiceResource, User principal) {
LOGGER.debug("Create a new resource {} for domain {}", newServiceResource, domain);
ServiceResource resource = new ServiceResource();
resource.setId(newServiceResource.getId() == null ? RandomString.generate() : newServiceResource.getId());
resource.setReferenceId(domain);
resource.setReferenceType(ReferenceType.DOMAIN);
resource.setName(newServiceResource.getName());
resource.setType(newServiceResource.getType());
resource.setConfiguration(newServiceResource.getConfiguration());
resource.setCreatedAt(new Date());
resource.setUpdatedAt(resource.getCreatedAt());
return serviceResourceRepository.create(resource).flatMap(resource1 -> {
// send sync event to refresh plugins that are using this resource
Event event = new Event(Type.RESOURCE, new Payload(resource1.getId(), resource1.getReferenceType(), resource1.getReferenceId(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(resource1));
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error occurs while trying to create a resource", ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to create a resource", ex));
});
}
use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.
the class TagServiceImpl method create.
@Override
public Single<Tag> create(NewTag newTag, String organizationId, User principal) {
LOGGER.debug("Create a new tag: {}", newTag);
String id = humanReadableId(newTag.getName());
return tagRepository.findById(id, organizationId).isEmpty().flatMap(empty -> {
if (!empty) {
throw new TagAlreadyExistsException(newTag.getName());
} else {
Tag tag = new Tag();
tag.setId(id);
tag.setOrganizationId(organizationId);
tag.setName(newTag.getName());
tag.setDescription(newTag.getDescription());
tag.setCreatedAt(new Date());
tag.setUpdatedAt(tag.getCreatedAt());
return tagRepository.create(tag);
}
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error occurs while trying to create a tag", ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to create a tag", ex));
}).doOnSuccess(tag -> auditService.report(AuditBuilder.builder(TagAuditBuilder.class).tag(tag).principal(principal).type(EventType.TAG_CREATED))).doOnError(throwable -> auditService.report(AuditBuilder.builder(TagAuditBuilder.class).referenceId(organizationId).principal(principal).type(EventType.TAG_CREATED).throwable(throwable)));
}
use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.
the class EmailTemplateServiceImpl method create0.
private Single<Email> create0(ReferenceType referenceType, String referenceId, String client, NewEmail newEmail, User principal) {
String emailId = RandomString.generate();
// check if email is unique
return checkEmailUniqueness(referenceType, referenceId, client, newEmail.getTemplate().template()).flatMap(irrelevant -> {
Email email = new Email();
email.setId(emailId);
email.setReferenceType(referenceType);
email.setReferenceId(referenceId);
email.setClient(client);
email.setEnabled(newEmail.isEnabled());
email.setTemplate(newEmail.getTemplate().template());
email.setFrom(newEmail.getFrom());
email.setFromName(newEmail.getFromName());
email.setSubject(newEmail.getSubject());
email.setContent(newEmail.getContent());
email.setExpiresAfter(newEmail.getExpiresAfter());
email.setCreatedAt(new Date());
email.setUpdatedAt(email.getCreatedAt());
return emailRepository.create(email);
}).flatMap(email -> {
// create event for sync process
Event event = new Event(Type.EMAIL, new Payload(email.getId(), email.getReferenceType(), email.getReferenceId(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(email));
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error occurs while trying to create a email", ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to create a email", ex));
}).doOnSuccess(email -> auditService.report(AuditBuilder.builder(EmailTemplateAuditBuilder.class).principal(principal).type(EventType.EMAIL_TEMPLATE_CREATED).email(email))).doOnError(throwable -> auditService.report(AuditBuilder.builder(EmailTemplateAuditBuilder.class).principal(principal).type(EventType.EMAIL_TEMPLATE_CREATED).throwable(throwable)));
}
Aggregations