Search in sources :

Example 1 with CRECausedByWrapper

use of com.laytonsmith.core.exceptions.CRE.CRECausedByWrapper in project CommandHelper by EngineHub.

the class ConfigRuntimeException method DoReport.

private static void DoReport(ConfigRuntimeException e, Environment env) {
    MCPlayer p = null;
    if (e.getEnv() != null && e.getEnv().getEnv(CommandHelperEnvironment.class).GetPlayer() != null) {
        p = e.getEnv().getEnv(CommandHelperEnvironment.class).GetPlayer();
    }
    List<StackTraceElement> st = new ArrayList<>();
    if (e instanceof AbstractCREException) {
        st = ((AbstractCREException) e).getCREStackTrace();
    }
    DoReport(e.getMessage(), AbstractCREException.getExceptionName(e), e, st, p);
    if (Prefs.DebugMode()) {
        if (e.getCause() != null && !(e.getCause() instanceof CRECausedByWrapper)) {
            // This is more of a system level exception, so if debug mode is on, we also want to print this stack trace
            StreamUtils.GetSystemErr().println("The previous MethodScript error had an attached cause:");
            e.getCause().printStackTrace(StreamUtils.GetSystemErr());
        }
        if (e.getTarget().equals(Target.UNKNOWN)) {
            // This should never happen, but there are still some hard to track
            // down bugs that cause this. If it does happen, we want to print out
            // a stacktrace from here, which *might* assist in fixing the error
            // messages to provide a proper target.
            StreamUtils.GetSystemErr().println("Since the exception has an unknown code target, here is additional information that may help:");
            StreamUtils.GetSystemErr().println(StackTraceUtils.GetStacktrace(new Exception()));
        }
    }
}
Also used : AbstractCREException(com.laytonsmith.core.exceptions.CRE.AbstractCREException) CRECausedByWrapper(com.laytonsmith.core.exceptions.CRE.CRECausedByWrapper) MCPlayer(com.laytonsmith.abstraction.MCPlayer) ArrayList(java.util.ArrayList) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) IOException(java.io.IOException) AbstractCREException(com.laytonsmith.core.exceptions.CRE.AbstractCREException)

Example 2 with CRECausedByWrapper

use of com.laytonsmith.core.exceptions.CRE.CRECausedByWrapper in project CommandHelper by EngineHub.

the class ConfigRuntimeException method DoReport.

/**
 * If the Reaction returned by GetReaction is to report the exception, this function should be used to standardize
 * the report format. If the error message wouldn't be very useful by itself, or if a hint is desired, an optional
 * message may be provided (null otherwise).
 *
 * @param e
 * @param optionalMessage
 */
@SuppressWarnings("ThrowableResultIgnored")
private static void DoReport(String message, String exceptionType, ConfigRuntimeException ex, List<StackTraceElement> stacktrace, MCPlayer currentPlayer) {
    String type = exceptionType;
    if (exceptionType == null) {
        type = "FATAL";
    }
    List<StackTraceElement> st = new ArrayList<>(stacktrace);
    if (message == null) {
        message = "";
    }
    if (!"".equals(message.trim())) {
        message = ": " + message;
    }
    Target top = Target.UNKNOWN;
    for (StackTraceElement e : st) {
        Target t = e.getDefinedAt();
        if (top == Target.UNKNOWN) {
            top = t;
        }
    }
    StringBuilder log = new StringBuilder();
    StringBuilder console = new StringBuilder();
    StringBuilder player = new StringBuilder();
    PrintMessage(log, console, player, type, message, ex, st);
    if (ex != null) {
        // Otherwise, a CCE
        if (ex.getCause() != null && ex.getCause() instanceof ConfigRuntimeException) {
            ex = (ConfigRuntimeException) ex.getCause();
        }
        while (ex instanceof CRECausedByWrapper) {
            Target t = ex.getTarget();
            log.append("Caused by:\n");
            console.append(TermColors.CYAN).append("Caused by:\n");
            player.append(MCChatColor.AQUA).append("Caused by:\n");
            CArray exception = ((CRECausedByWrapper) ex).getException();
            CArray stackTrace = Static.getArray(exception.get("stackTrace", t), t);
            List<StackTraceElement> newSt = new ArrayList<>();
            for (Construct consElement : stackTrace.asList()) {
                CArray element = Static.getArray(consElement, t);
                int line = Static.getInt32(element.get("line", t), t);
                File file = new File(element.get("file", t).val());
                int col = element.getColumn();
                Target stElementTarget = new Target(line, file, col);
                newSt.add(new StackTraceElement(element.get("id", t).val(), stElementTarget));
            }
            String nType = exception.get("classType", t).val();
            String nMessage = exception.get("message", t).val();
            if (!"".equals(nMessage.trim())) {
                nMessage = ": " + nMessage;
            }
            PrintMessage(log, console, player, nType, nMessage, ex, newSt);
            ex = (ConfigRuntimeException) ex.getCause();
        }
    }
    // Log
    // Don't log to screen though, since we're ALWAYS going to do that ourselves.
    CHLog.GetLogger().Log("COMPILE ERROR".equals(exceptionType) ? CHLog.Tags.COMPILER : CHLog.Tags.RUNTIME, LogLevel.ERROR, log.toString(), top, false);
    // Console
    StreamUtils.GetSystemOut().println(console.toString() + TermColors.reset());
    // Player
    if (currentPlayer != null) {
        currentPlayer.sendMessage(player.toString());
    }
}
Also used : Target(com.laytonsmith.core.constructs.Target) CRECausedByWrapper(com.laytonsmith.core.exceptions.CRE.CRECausedByWrapper) ArrayList(java.util.ArrayList) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) File(java.io.File)

Aggregations

CRECausedByWrapper (com.laytonsmith.core.exceptions.CRE.CRECausedByWrapper)2 ArrayList (java.util.ArrayList)2 MCPlayer (com.laytonsmith.abstraction.MCPlayer)1 CArray (com.laytonsmith.core.constructs.CArray)1 Construct (com.laytonsmith.core.constructs.Construct)1 Target (com.laytonsmith.core.constructs.Target)1 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)1 AbstractCREException (com.laytonsmith.core.exceptions.CRE.AbstractCREException)1 File (java.io.File)1 IOException (java.io.IOException)1