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

cz.mmsparams.api.utils.ExceptionHelper Maven / Gradle / Ivy

The newest version!
package cz.mmsparams.api.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

import cz.mmsparams.api.exceptions.MmsParamsExceptionBase;
import cz.mmsparams.api.exceptions.SystemException;
import cz.mmsparams.api.websocket.messages.errors.GenericErrorResponseMessage;

public class ExceptionHelper
{
    private ExceptionHelper()
    {
    }

    public static String getStackTrace(Throwable exception)
    {
        if (exception == null)
            return "";

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        exception.printStackTrace(pw);
        return sw.toString(); // stack trace as a string
    }

    public static Exception generateException(GenericErrorResponseMessage error)
    {
        if (error == null)
            return new Exception("GenericErrorResponseMessage not present");

        return new Exception(error.toString(), error.getTestErrorModel().getException());
    }

    public static  T castException(Throwable ex)
    {
        if (InheritanceUtil.isSubclass(ex, MmsParamsExceptionBase.class))
        {
            return (T) ex;
        }
        else
        {
            return (T) new SystemException(ex);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy