Class ErrorHandler

All Implemented Interfaces:
EventEmitterInterface

public final class ErrorHandler extends CapableEventEmitter<Boolean>
Centralized error handling mechanism for J3Engine.

This class provides methods to handle various types of J3DErrors, categorising them by their severity (mild, warning, fatal) and performing appropriate actions such as logging, displaying user messages, or terminating the application. It also supports temporarily ignoring specific error types.

Author:
Lehlogonolo Poole
See Also:
  • Field Details

  • Constructor Details

    • ErrorHandler

      public ErrorHandler()
  • Method Details

    • ignore

      public <T extends J3DError> void ignore(Class<T> err)
      Registers a specific J3DError class to be ignored by the error handler. When an error of the registered class type is encountered by the handle(J3DError) method, it will be consumed (removed from the ignore list) and no further action will be taken. This effectively makes the next occurrence of that specific error type to be silently handled.
      Type Parameters:
      T - The type of J3DError to ignore.
      Parameters:
      err - The Class object of the J3DError to be ignored.
      Throws:
      RuntimeException - if more than 10 error classes are attempted to be ignored.
    • handle

      public boolean handle(@NotNull J3DError err)
      Handles a given J3DError, processing it based on its type and severity.

      If the error is registered to be ignored, that is handled

      Otherwise, the error is processed based on its specific type:

      • J3DMild: The error is printed to the application's log.
      • J3DWarning: The error is printed to the application's log, and a warning message dialogue is displayed to the user.
      • J3DFatal: The error is printed to the application's log, and a fatal error message dialogue is displayed to the user. If the fatal error's terminate() method returns true, an additional message is shown, and the application is terminated via System.exit(1).
      • Any other J3DError type (that is also an instance of J3ErrSeverity): The error is simply printed to the application's log.
      Parameters:
      err - The J3DError instance to be handled.
    • handleThenThrow

      public void handleThenThrow(J3DError err) throws J3DError
      Handles a given J3DError using the handle(J3DError) method and then re-throws it.
      Parameters:
      err - The J3DError instance to be handled and thrown.
      Throws:
      J3DError - The same J3DError instance that was passed in, after it has been processed by the handle(J3DError) method.
    • emitThenThrow

      public void emitThenThrow(J3DError err) throws J3DError
      Broadcasts an EventType.EMITTED_ERROR event to registered listeners before re-throwing the error.

      Unlike handleThenThrow(J3DError), this method uses CapableEventEmitter to query registered CapableEventListener instances via CapableEventEmitter.capableBroadcast(EventType, EventPayload).

      This allows listeners to intercept the error and return a Boolean response indicating whether standard handling should be suppressed or executed:

      If the listener return a boolean true result, the error is considered handled and is never thrown

      Parameters:
      err - The J3DError instance to be emitted and thrown.
      Throws:
      J3DError - The error passed in, thrown after event emission and handling.
      See Also: