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

com.icthh.xm.commons.exceptions.BusinessException Maven / Gradle / Ivy

There is a newer version: 4.0.12
Show newest version
package com.icthh.xm.commons.exceptions;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * Custom, parametrized exception, which can be translated on the client side.
 * For example:
 * 

*

 * throw new BusinessException("myCustomError", "hello", "world");
 * 
*

* Can be translated with: *

*

 * "error.myCustomError" :  "The server says {{param0}} to {{param1}}"
 * 
*/ @Getter public class BusinessException extends RuntimeException { private static final long serialVersionUID = 1L; private static final String PARAM = "param"; private final String code; private final String message; private final Map paramMap = new HashMap<>(); public BusinessException(String message) { this(ErrorConstants.ERR_BUSINESS, message); } public BusinessException(String code, String message) { super(message); this.code = code; this.message = message; } public BusinessException(String code, String message, Map paramMap) { super(message); this.code = code; this.message = message; this.paramMap.putAll(paramMap); } public BusinessException(String message, Map paramMap) { this(message); this.paramMap.putAll(paramMap); } public BusinessException withParams(String... params) { if (params != null && params.length > 0) { for (int i = 0; i < params.length; i++) { paramMap.put(PARAM + i, params[i]); } } return this; } @Override public String toString() { return "{code=" + code + ", message=" + message + (paramMap.isEmpty() ? "" : ", paramMap=" + paramMap) + "}"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy