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

com.iiifi.kite.boot.common.error.BaseExceptionTranslator Maven / Gradle / Ivy

/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.boot.common.error;

import java.util.Set;

import javax.validation.ConstraintViolation;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import com.iiifi.kite.core.result.Result;
import com.iiifi.kite.core.result.SystemCode;

/**
 * 抽取共性 通用部分代码
 *
 * @author [email protected] 花朝
 */
public abstract class BaseExceptionTranslator {

    /**
     * 处理 BindingResult
     *
     * @param result BindingResult
     * @return Result
     */
    protected Result handleError(BindingResult result) {
        FieldError error = result.getFieldError();
        return Result.fail(SystemCode.PARAM_BIND_ERROR, error.getDefaultMessage());
    }

    /**
     * 处理 ConstraintViolation
     *
     * @param violations 校验结果
     * @return Result
     */
    protected Result handleError(Set> violations) {
        ConstraintViolation violation = violations.iterator().next();
        return Result.fail(SystemCode.PARAM_VALID_ERROR, violation.getMessage());
    }
}