001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.exception;
017
018import com.mybatisflex.core.exception.locale.Localizable;
019
020/**
021 * MybatisFlexException 异常封装类
022 */
023public final class FlexExceptions {
024
025    private FlexExceptions() {
026    }
027
028
029    /**
030     * 封装 MybatisFlexException 异常
031     *
032     * @param throwable 异常
033     * @return MybatisFlexException
034     */
035    public static MybatisFlexException wrap(Throwable throwable) {
036        if (throwable instanceof MybatisFlexException) {
037            return (MybatisFlexException) throwable;
038        }
039        return new MybatisFlexException(throwable);
040    }
041
042
043    /**
044     * 封装 MybatisFlexException 异常
045     *
046     * @param throwable 异常
047     * @param msg       消息
048     * @param params    消息参数
049     * @return MybatisFlexException
050     */
051    public static MybatisFlexException wrap(Throwable throwable, String msg, Object... params) {
052        return new MybatisFlexException(String.format(msg, params), throwable);
053    }
054
055    /**
056     * 封装 MybatisFlexException 异常
057     *
058     * @param msg    消息
059     * @param params 消息参数
060     * @return MybatisFlexException
061     */
062    public static MybatisFlexException wrap(String msg, Object... params) {
063        return new MybatisFlexException(String.format(msg, params));
064    }
065
066    public static MybatisFlexException wrap(Throwable cause, Localizable pattern, Object... args) {
067        return new MybatisFlexException(cause, pattern, args);
068    }
069
070    public static MybatisFlexException wrap(Localizable pattern, Object... args) {
071        return new MybatisFlexException(pattern, args);
072    }
073
074}