All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.capitalone.dashboard.model.ErrorResponse Maven / Gradle / Ivy

There is a newer version: 3.4.53
Show newest version
package com.capitalone.dashboard.model;

import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class ErrorResponse {
    private Map> globalErrors = new HashMap>();
    private Map> fieldErrors = new HashMap>();

    public Map> getGlobalErrors() {
        return globalErrors;
    }

    public Map> getFieldErrors() {
        return fieldErrors;
    }

    public void addFieldError(String field, String error) {
        List errors = getFieldErrors().get(field);
        if (errors == null) {
            errors = new ArrayList();
            getFieldErrors().put(field, errors);
        }
        errors.add(error);
    }

    public static ErrorResponse fromBindException(BindException bindException) {
        ErrorResponse errorResponse = new ErrorResponse();

        for (ObjectError objectError : bindException.getGlobalErrors()) {
            List errors = errorResponse.getGlobalErrors().get(objectError.getObjectName());
            if (errors == null) {
                errors = new ArrayList();
                errorResponse.getGlobalErrors().put(objectError.getObjectName(), errors);
            }
            errors.add(objectError.getDefaultMessage());
        }

        for (FieldError fieldError : bindException.getFieldErrors()) {
            List errors = errorResponse.getFieldErrors().get(fieldError.getField());
            if (errors == null) {
                errors = new ArrayList();
                errorResponse.getFieldErrors().put(fieldError.getField(), errors);
            }
            errors.add(fieldError.getDefaultMessage());
        }

        return errorResponse;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy