Example usage for org.springframework.dao DataIntegrityViolationException getRootCause

List of usage examples for org.springframework.dao DataIntegrityViolationException getRootCause

Introduction

In this page you can find the example usage for org.springframework.dao DataIntegrityViolationException getRootCause.

Prototype

@Nullable
public Throwable getRootCause() 

Source Link

Document

Retrieve the innermost cause of this exception, if any.

Usage

From source file:com.todo.backend.web.rest.exception.ExceptionResolver.java

@ResponseStatus(value = HttpStatus.CONFLICT)
@ExceptionHandler(DataIntegrityViolationException.class)
public @ResponseBody ErrorResponse dataIntegrityException(HttpServletRequest request,
        DataIntegrityViolationException exception) {
    if (log.isErrorEnabled()) {
        log.error(exception.getMessage(), exception);
    }/*from w  ww .j  a v  a 2 s. c o m*/
    // example: Duplicate entry 'field1-field2' for key 'UNQ_MODE_F1_F2_251F9D'
    final String message = exception.getRootCause().getMessage();
    if (message.contains("'UNQ")) {
        final String constraint = message.substring(message.indexOf("'UNQ") + 1, message.lastIndexOf("'"));
        return new ErrorResponse(ConstraintMapping.getErrorCodeForConstraint(constraint), message);
    }
    return new ErrorResponse(message, Collections.emptyList());
}

From source file:net.maritimecloud.identityregistry.controllers.EntityController.java

/**
 * Creates a new Entity//from  www. j a v  a 2s.c o  m
 *
 * @return a reply...
 * @throws McBasicRestException
 */
protected ResponseEntity<T> createEntity(HttpServletRequest request, String orgMrn, T input)
        throws McBasicRestException {
    Organization org = this.organizationService.getOrganizationByMrn(orgMrn);
    if (org != null) {
        // Check that the entity being created belongs to the organization
        if (!MrnUtil.getOrgShortNameFromOrgMrn(orgMrn)
                .equals(MrnUtil.getOrgShortNameFromEntityMrn(input.getMrn()))) {
            throw new McBasicRestException(HttpStatus.BAD_REQUEST, MCIdRegConstants.MISSING_RIGHTS,
                    request.getServletPath());
        }
        input.setIdOrganization(org.getId());
        try {
            T newEntity = this.entityService.save(input);
            return new ResponseEntity<>(newEntity, HttpStatus.OK);
        } catch (DataIntegrityViolationException e) {
            throw new McBasicRestException(HttpStatus.CONFLICT, e.getRootCause().getMessage(),
                    request.getServletPath());
        }
    } else {
        throw new McBasicRestException(HttpStatus.NOT_FOUND, MCIdRegConstants.ORG_NOT_FOUND,
                request.getServletPath());
    }
}

From source file:net.maritimecloud.identityregistry.controllers.OrganizationController.java

/**
 * Receives an application for a new organization and root-user
 * /*from   w ww  .j av  a  2  s.  co  m*/
 * @return a reply...
 * @throws McBasicRestException 
 */
@RequestMapping(value = "/api/org/apply", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public ResponseEntity<Organization> applyOrganization(HttpServletRequest request,
        @RequestBody @Valid Organization input, BindingResult bindingResult) throws McBasicRestException {
    ValidateUtil.hasErrors(bindingResult, request);
    // Make sure all mrn are lowercase
    input.setMrn(input.getMrn().trim().toLowerCase());
    input.setApproved(false);
    // If no federation type is set we for now default to "test-idp"
    if (input.getFederationType() == null || input.getFederationType().isEmpty()) {
        input.setFederationType("test-idp");
    }
    Organization newOrg;
    try {
        newOrg = this.organizationService.save(input);
    } catch (DataIntegrityViolationException e) {
        throw new McBasicRestException(HttpStatus.BAD_REQUEST, e.getRootCause().getMessage(),
                request.getServletPath());
    }
    // Send email to organization saying that the application is awaiting approval
    emailUtil.sendOrgAwaitingApprovalEmail(newOrg.getEmail(), newOrg.getName());
    // Send email to admin saying that an Organization is awaiting approval
    emailUtil.sendAdminOrgAwaitingApprovalEmail(newOrg.getName());
    return new ResponseEntity<>(newOrg, HttpStatus.OK);
}

From source file:net.jkratz.igdb.controller.advice.ErrorController.java

@RequestMapping(produces = "application/json")
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseStatus(value = HttpStatus.CONFLICT)
public @ResponseBody Map<String, Object> handleDataIntegrityViolationException(
        DataIntegrityViolationException ex) throws IOException {
    logger.error("Data Integrity Error", ex);
    Map<String, Object> map = Maps.newHashMap();
    map.put("error", "Data Integrity Error");
    map.put("message", ex.getMessage());
    if (ex.getRootCause() != null) {
        map.put("cause", ex.getRootCause().getMessage());
    }//  w ww  .  j  a va2  s.  com
    return map;
}

From source file:jp.co.opentone.bsol.linkbinder.dao.impl.UserDaoImpl.java

/**
 * ???????.//from  w ww.ja v a  2s .  co  m
 * <p>
 * ????{@link AuthenticateException}????
 * ????????????throw?.
 * </p>
 * @param e ?????
 * @throws AuthenticateException ??
 */
private void handleExceptionAtAuthentication(DataIntegrityViolationException e) throws AuthenticateException {
    Throwable cause = e.getRootCause();
    if (cause instanceof SQLException && ((SQLException) cause).getErrorCode() == ERROR_AUTHENTICATION_FAILED) {
        throw new AuthenticateException();
    } else {
        throw e;
    }
}