001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
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 org.kuali.common.util.base;
017
018import static java.lang.String.format;
019
020import java.io.IOException;
021import java.io.PrintWriter;
022import java.io.StringWriter;
023
024/**
025 * <p>
026 * Utility methods for creating exceptions with richly formatted error messages.
027 *
028 * <p>
029 * Utility method for generating the stacktrace of an exception as a string.
030 *
031 * <p>
032 * Example usage:
033 *
034 * <pre>
035 * throw Exceptions.illegalArgument(&quot;port must be &gt;= %s and &lt;= %s&quot;, 0, 65535);
036 * </pre>
037 */
038public final class Exceptions {
039
040    public static String printStackTraceToString(Throwable throwable) {
041        StringWriter sw = new StringWriter();
042        throwable.printStackTrace(new PrintWriter(sw));
043        return sw.toString();
044    }
045
046    public static IOException newIOException(Throwable cause) {
047        return new IOException(cause);
048    }
049
050    public static IOException newIOException(String msg) {
051        return new IOException(msg);
052    }
053
054    public static IOException newIOException(String msg, Object... args) {
055        return new IOException(formatted(msg, args));
056    }
057
058    public static IOException newIOException(Throwable cause, String msg, Object... args) {
059        return new IOException(formatted(msg, args), cause);
060    }
061
062    public static IllegalStateException illegalState(Throwable cause) {
063        return new IllegalStateException(cause);
064    }
065
066    public static IllegalStateException illegalState(String msg) {
067        return new IllegalStateException(msg);
068    }
069
070    public static IllegalStateException illegalState(String msg, Object... args) {
071        return new IllegalStateException(formatted(msg, args));
072    }
073
074    public static IllegalStateException illegalState(Throwable cause, String msg, Object... args) {
075        return new IllegalStateException(formatted(msg, args), cause);
076    }
077
078    public static IllegalArgumentException illegalArgument(Throwable cause) {
079        return new IllegalArgumentException(cause);
080    }
081
082    public static IllegalArgumentException illegalArgument(String msg) {
083        return new IllegalArgumentException(msg);
084    }
085
086    public static IllegalArgumentException illegalArgument(String msg, Object... args) {
087        return new IllegalArgumentException(formatted(msg, args));
088    }
089
090    public static IllegalArgumentException illegalArgument(Throwable cause, String msg, Object... args) {
091        return new IllegalArgumentException(formatted(msg, args), cause);
092    }
093
094    private static String formatted(String msg, Object... args) {
095        return (args == null || args.length == 0) ? msg : format(msg, args);
096    }
097
098}