001/* 002 * Copyright (c) 2022-2025, 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 020import java.text.MessageFormat; 021import java.util.Locale; 022 023/** 024 * @author michael 025 * @author 王帅 026 */ 027public class MybatisFlexException extends RuntimeException { 028 029 private static final long serialVersionUID = 1L; 030 031 private Localizable pattern; 032 private Object[] arguments; 033 034 public MybatisFlexException(Throwable cause, Localizable pattern, Object[] arguments) { 035 super(cause); 036 this.pattern = pattern; 037 this.arguments = arguments; 038 } 039 040 public MybatisFlexException(Localizable pattern, Object... arguments) { 041 this.pattern = pattern; 042 this.arguments = arguments; 043 } 044 045 public MybatisFlexException(String message) { 046 super(message); 047 } 048 049 public MybatisFlexException(String message, Throwable cause) { 050 super(message, cause); 051 } 052 053 public MybatisFlexException(Throwable cause) { 054 super(cause); 055 } 056 057 @Override 058 public String getMessage() { 059 return getMessage(Locale.CHINESE); 060 } 061 062 @Override 063 public String getLocalizedMessage() { 064 return getMessage(Locale.getDefault()); 065 } 066 067 private String getMessage(Locale locale) { 068 if (pattern == null) { 069 return super.getMessage(); 070 } 071 String localizedString = pattern.getLocalizedString(locale); 072 return MessageFormat.format(localizedString, arguments); 073 } 074 075}