Class NaftahBugError

All Implemented Interfaces:
Serializable

public class NaftahBugError extends AssertionError
This class represents an error that is thrown when a bug is recognized inside the runtime. Basically it is thrown when a constraint is not fulfilled that should be fulfilled.

It extends AssertionError, indicating a serious, unexpected condition that the program should never reach under normal circumstances.

Optionally carries a cause Throwable and a custom bug message.

The error message is prefixed with Arabic messages like "خطأ برمجي!" to indicate developer or logic-level bugs.

Author:
Chakib Daii
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serialization ID.
      See Also:
    • exception

      private final Throwable exception
      The underlying exception that caused this error, if any.
    • line

      private int line
      The line number on which the 1st character of this token was matched, line=1..n.
    • column

      private int column
      The index of the first character of this token relative to the beginning of the line at which it occurs, 0..n-1.
    • message

      private String message
      The descriptive message text for the bug error.
  • Constructor Details

    • NaftahBugError

      public NaftahBugError(String message)
      Constructs a NaftahBugError using the given message text.
      Parameters:
      message - the error message describing the bug
    • NaftahBugError

      public NaftahBugError(String msg, int line, int column)
      Constructs a NaftahBugError using a descriptive message and the line number along with the column.
      Parameters:
      msg - the error message describing the bug
      line - The line number on which the 1st character of this token was matched
      column - The index of the first character of this token relative to the beginning of the line at which it occurs
    • NaftahBugError

      public NaftahBugError(Throwable exception)
      Constructs a NaftahBugError using the given cause.
      Parameters:
      exception - the underlying cause of this error
    • NaftahBugError

      public NaftahBugError(String msg, Throwable exception)
      Constructs a NaftahBugError using both a descriptive message and an underlying exception cause.
      Parameters:
      msg - the error message describing the bug
      exception - the underlying cause of this error
    • NaftahBugError

      public NaftahBugError(String msg, Throwable exception, int line, int column)
      Constructs a NaftahBugError using a descriptive message and an underlying exception cause and the line number along with the column.
      Parameters:
      msg - the error message describing the bug
      exception - the underlying cause of this error
      line - The line number on which the 1st character of this token was matched
      column - The index of the first character of this token relative to the beginning of the line at which it occurs
  • Method Details

    • toString

      public String toString()
      Returns the string representation of this error, which delegates to getMessage().
      Overrides:
      toString in class Throwable
      Returns:
      the error message
    • getMessage

      public String getMessage()
      Returns the detailed error message. If a message was provided during construction, it will be returned prefixed with "خطأ برمجي!". Otherwise, it will return the message from the exception cause, prefixed with "خطأ برمجي! استثناء غير ملتقط: " and localized using ExceptionLocalizer.localizeException(Throwable).
      Overrides:
      getMessage in class Throwable
      Returns:
      the formatted bug message
    • doGetMessage

      private String doGetMessage()
      Constructs the internal message based on the presence of a user-defined message and an optional exception.
      Returns:
      the constructed error detail string
    • getCause

      public Throwable getCause()
      Returns the underlying cause of this bug error, if available.
      Overrides:
      getCause in class Throwable
      Returns:
      the exception that caused this bug, or null if none
    • getBugText

      public String getBugText()
      Returns the raw bug message text without formatting or localization.
      Returns:
      the original bug message if set, otherwise the cause's message
    • setBugText

      public void setBugText(String msg)
      Updates the descriptive bug message.
      Parameters:
      msg - the new bug message
    • hasCodePosition

      public boolean hasCodePosition()
      Checks whether the bug error has an associated line and column position.

      This method returns true if both the line and column numbers are set to valid values (i.e., not equal to -1), indicating the presence of a code position in the error. Otherwise, it returns false.

      Returns:
      true if the error has a valid line and column; false otherwise.
    • setLine

      public void setLine(int line)
      Sets the line number where the bug was detected.

      This method allows the line number to be specified for the bug error. The line number should be greater than 0, and it must be different from the column number for a valid update. If the provided line is -1, no update will occur.

      Parameters:
      line - The line number where the bug occurred (1-based index).
      Throws:
      IllegalArgumentException - If the line number is -1.
    • setColumn

      public void setColumn(int column)
      Sets the column number where the bug was detected.

      This method allows the column number to be specified for the bug error. The column number should be greater than or equal to 0, and it must be different from the line number for a valid update. If the provided column is -1, no update will occur.

      Parameters:
      column - The column index (0-based) where the bug occurred.
      Throws:
      IllegalArgumentException - If the column number is -1.