Class ExceptionUtils
This class provides static methods to retrieve the root cause or the most specific cause
of an exception, as well as a factory method to create a custom NaftahBugError
related to invalid loop labels.
Note: This class cannot be instantiated. Attempting to instantiate
it will always throw a NaftahBugError.
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringError message indicating that a single argument is empty.static final StringError message indicating that multiple arguments are empty.static final StringError message indicating an overflow in a decimal number.static final BiFunction<String,String, String> Generates an error message indicating that an instance method was called incorrectly.static final StringError message indicating that the value is Not-a-Number (NaN).static final StringNote prefix used in error messages.static final StringError message indicating that bitwise operations are not supported on decimal (floating point) numbers. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic ThrowablegetMostSpecificCause(Throwable original) Returns the most specific cause of the givenThrowable.static ThrowablegetRootCause(Throwable original) Returns the root cause of the givenThrowable.static IllegalArgumentExceptionnewIllegalArgumentException(Object object) Creates anIllegalArgumentExceptionindicating that a value has an unsupported runtime type.static IllegalArgumentExceptionnewIllegalArgumentException(Object object, Object object1) Creates anIllegalArgumentExceptionindicating that a combination of values has unsupported or incompatible runtime types.static IllegalAccessExceptionnewIllegalFieldAccessException(String fieldName) Creates anIllegalAccessExceptionindicating that a reflective write to a constant (final/static) field was ignored by the JVM.static NaftahBugErrornewNaftahBugInvalidLoopLabelError(String label, org.antlr.v4.runtime.Parser parser, int line, int column) Creates a newNaftahBugErrorindicating an invalid reuse of a loop label.static NaftahBugErrornewNaftahBugInvalidNumberConversionOverflowError(boolean overflow, Number number, Class<?> targetClass) Raise an overflow exception for the given number and target class.static NaftahBugErrorCreates a newNaftahBugErrorindicating that the given object represents an invalid numeric value.static NaftahBugErrornewNaftahBugInvalidNumberValueError(Object object, int radix) Creates a newNaftahBugErrorfor an invalid numeric value in a specific radix.static NaftahBugErrornewNaftahBugInvalidNumberValueError(Object object, int radix, Exception exception) Creates a newNaftahBugErrorfor an invalid numeric value in a specific radix, including the underlying cause.static NaftahBugErrornewNaftahBugInvalidNumberValueError(Object object, Exception exception) Creates a newNaftahBugErrorfor an invalid numeric value, with the cause.static NaftahBugErrorCreates a newNaftahBugErrorindicating that an invalid usage has occurred.static NaftahBugErrornewNaftahBugNullInputError(boolean singleInput, Object... args) Creates a newNaftahBugErrorindicating that a required input argument is null or missing.static NaftahBugErrornewNaftahBugUnsupportedBitwiseDecimalError(boolean singleInput, DynamicNumber... dn) Creates a NaftahBugError indicating that bitwise operations on decimal numbers are unsupported.static NaftahBugErrornewNaftahBugUnsupportedNumbersError(boolean singleInput, DynamicNumber... dn) Creates a newNaftahBugErrorindicating that one or more numeric types are unsupported.static NaftahBugErrornewNaftahExpressionsDeclarationsSizeMismatchErrorError(int line, int column) Creates and throws aNaftahBugErrorwhen the number of assignment expressions does not match the number of declared variables.static NaftahBugErrornewNaftahIllegalArgumentError(String functionName, String providerClassName, int paramCount, int argCount, String functionDetailedString, Exception e, int line, int column) Creates a newNaftahBugErrorfor illegal argument count or mismatched argument types.static NaftahBugErrornewNaftahInstantiationError(String functionName, String functionDetailedString, Exception e, int line, int column) Creates a newNaftahBugErrorwhen object instantiation fails.static <T extends JvmExecutable>
NaftahBugErrornewNaftahInvocableListFoundError(String functionName, Collection<T> functionsOrClassInitializers, Throwable exception, int line, int column) Creates and throws aNaftahBugErrorwhen a collection of invocable functions or class initializers is found, but the caller did not specify an index to select which one to invoke.static NaftahBugErrornewNaftahInvocableNotFoundError(String functionName) Creates a newNaftahBugErrorfor a missing invocable without specifying source line or column.static NaftahBugErrornewNaftahInvocableNotFoundError(String functionName, int line, int column) Creates and throws a newNaftahBugErrorwhen the requested invocable (method or constructor) cannot be found in the current context.static NaftahBugErrornewNaftahInvocationError(String functionName, String functionDetailedString, Exception e, int line, int column) Creates a newNaftahBugErrorfor errors occurring during function invocation.static NaftahBugErrorCreates a newNaftahBugErrorindicating that the specified key was not found in the associative array.static NaftahBugErrorCreates a newNaftahBugErrorindicating that negative numbers are not allowed.static NaftahBugErrornewNaftahNonInvocableFunctionError(String functionName, String functionDetailedString, int line, int column) Creates a newNaftahBugErrorfor functions that are not invocable.static NaftahBugErrorCreates aNaftahBugErrorindicating that an attempt was made to reassign a constant (final or otherwise immutable) variable.static NaftahBugErrornewNaftahSettingConstantError(String name, Throwable th) Creates aNaftahBugErrorindicating that an attempt was made to reassign a constant (final or otherwise immutable) variable, optionally wrapping the underlying cause.static NaftahBugErrornewNaftahSingleExpressionAssignmentError(int line, int column) Creates and throws aNaftahBugErrorwhen an assignment expression is invalid because it is not provided as a tuple of elements.static NaftahBugErrornewNaftahSpecifiedTypesExceedVariableNamesError(int line, int column) Creates and throws aNaftahBugErrorwhen the parser encounters multiple variable declarations where the number of explicitly specified types exceeds the number of declared variable names.static NaftahBugErrornewNaftahTypeMismatchError(String name, String type, int line, int column) Creates aNaftahBugErrorrepresenting a type-mismatch error with source location information.static NaftahBugErrornewNaftahUnsupportedFunctionError(String functionName, Class<?> functionClass, int line, int column) Creates a newNaftahBugErrorindicating that the specified function is not supported.
-
Field Details
-
INFINITE_DECIMAL_ERROR
Error message indicating an overflow in a decimal number. The value provided is infinite and thus invalid. Example message format: "تجاوز في العدد العشري: القيمة غير نهائية للمدخل 'value'."- See Also:
-
NAN_DECIMAL_ERROR
Error message indicating that the value is Not-a-Number (NaN). Example message format: "القيمة ليست رقمًا (NaN): 'value'"- See Also:
-
UNSUPPORTED_BITWISE_DECIMAL_ERROR
Error message indicating that bitwise operations are not supported on decimal (floating point) numbers. The message includes the value and the attempted operation. Example message format: "العمليات الثنائية (bitwise) غير مدعومة على الأعداد ذات الفاصلة العشرية: 'value' operation،"- See Also:
-
EMPTY_ARGUMENT_ERROR
Error message indicating that a single argument is empty. Arabic: "لا يمكن أن يكون الوسيط فارغًا."- See Also:
-
EMPTY_ARGUMENTS_ERROR
Error message indicating that multiple arguments are empty. Arabic: "لا يمكن أن تكون الوسائط فارغة."- See Also:
-
NOTE
Note prefix used in error messages.Arabic: "ملاحظة:"
- See Also:
-
INVALID_INSTANCE_METHOD_CALL_MSG
Generates an error message indicating that an instance method was called incorrectly.The message is in Arabic and explains that the method is not static and requires the first argument (or second if the method is from a list of functions) to be the instance on which the method will be invoked.
Usage example:
String msg = INVALID_INSTANCE_METHOD_CALL_MSG.apply("methodName", additionalInfo);Parameters:
- First: the method name (
%s) - Second: additional context or details (
%s)
- First: the method name (
-
-
Constructor Details
-
ExceptionUtils
private ExceptionUtils()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
getRootCause
Returns the root cause of the givenThrowable.Traverses the causal chain to find the deepest cause of the throwable. If the original throwable is
null, returnsnull.- Parameters:
original- the throwable to inspect- Returns:
- the root cause throwable, or
nullif none found or iforiginalisnull
-
getMostSpecificCause
Returns the most specific cause of the givenThrowable.This method returns the deepest cause (root cause) if available, otherwise returns the original throwable.
- Parameters:
original- the throwable to inspect- Returns:
- the most specific cause (root cause if present) or the original throwable
-
newNaftahBugInvalidLoopLabelError
public static NaftahBugError newNaftahBugInvalidLoopLabelError(String label, org.antlr.v4.runtime.Parser parser, int line, int column) Creates a newNaftahBugErrorindicating an invalid reuse of a loop label.Constructs an error message in Arabic describing that the same loop label cannot be used in the specified statement.
- Parameters:
label- the loop label name that was reusedparser- the parser instance used to obtain vocabulary information- Returns:
- a new
NaftahBugErrorwith a localized error message
-
newNaftahBugInvalidUsageError
Creates a newNaftahBugErrorindicating that an invalid usage has occurred.- Returns:
- a
NaftahBugErrordescribing the invalid usage error
-
newNaftahBugNullInputError
Creates a newNaftahBugErrorindicating that a required input argument is null or missing.This method selects the appropriate error message depending on whether the error is related to a single input or multiple inputs.
- Parameters:
singleInput-trueif the error is due to a single missing input argument;falseif multiple input arguments are missing.- Returns:
- a
NaftahBugErrorinstance with the corresponding error message.
-
newNaftahBugInvalidNumberConversionOverflowError
public static NaftahBugError newNaftahBugInvalidNumberConversionOverflowError(boolean overflow, Number number, Class<?> targetClass) Raise an overflow exception for the given number and target class.- Parameters:
number- the number we tried to converttargetClass- the target class we tried to convert to- Throws:
IllegalArgumentException- if there is an overflow
-
newNaftahBugInvalidNumberValueError
Creates a newNaftahBugErrorindicating that the given object represents an invalid numeric value.- Parameters:
object- the invalid numeric value- Returns:
- a
NaftahBugErrordescribing the invalid number value error
-
newNaftahBugInvalidNumberValueError
public static NaftahBugError newNaftahBugInvalidNumberValueError(Object object, Exception exception) Creates a newNaftahBugErrorfor an invalid numeric value, with the cause.This method provides more detail by attaching the underlying
Exceptionthat caused the failure (e.g.,NumberFormatException).- Parameters:
object- the invalid numeric valueexception- the underlying exception (can benull)- Returns:
- a
NaftahBugErrordescribing the error with the given cause
-
newNaftahBugInvalidNumberValueError
Creates a newNaftahBugErrorfor an invalid numeric value in a specific radix.This is useful when parsing fails for a given base (e.g., base 16, base 10), and you want to include the radix in the error message.
- Parameters:
object- the invalid numeric valueradix- the number system base (e.g., 10 for decimal)- Returns:
- a
NaftahBugErrordescribing the error for the specified radix
-
newNaftahBugInvalidNumberValueError
public static NaftahBugError newNaftahBugInvalidNumberValueError(Object object, int radix, Exception exception) Creates a newNaftahBugErrorfor an invalid numeric value in a specific radix, including the underlying cause.This is the most detailed version, allowing you to specify the erroneous value, the radix used during parsing, and the original exception that triggered the failure.
- Parameters:
object- the invalid numeric valueradix- the number system base in which parsing was attemptedexception- the root exception that caused the error (nullable)- Returns:
- a
NaftahBugErrordescribing the full error context
-
newNaftahBugUnsupportedNumbersError
public static NaftahBugError newNaftahBugUnsupportedNumbersError(boolean singleInput, DynamicNumber... dn) Creates a newNaftahBugErrorindicating that one or more numeric types are unsupported.- Parameters:
singleInput- whether the error is for a single input (true) or multiple inputs (false)dn- the dynamic numbers involved in the error- Returns:
- a
NaftahBugErrordescribing the unsupported number type(s) error
-
newNaftahBugUnsupportedBitwiseDecimalError
public static NaftahBugError newNaftahBugUnsupportedBitwiseDecimalError(boolean singleInput, DynamicNumber... dn) Creates a NaftahBugError indicating that bitwise operations on decimal numbers are unsupported.- Parameters:
dn- the decimal numbers on which the bitwise operation was attempted- Returns:
- a NaftahBugError with an explanatory message
-
newNaftahKeyNotFoundError
Creates a newNaftahBugErrorindicating that the specified key was not found in the associative array.- Parameters:
key- the key that was not found- Returns:
- a
NaftahBugErrordescribing the missing key error
-
newNaftahNegativeNumberError
Creates a newNaftahBugErrorindicating that negative numbers are not allowed.- Returns:
- a
NaftahBugErrordescribing the disallowed negative number error
-
newNaftahUnsupportedFunctionError
public static NaftahBugError newNaftahUnsupportedFunctionError(String functionName, Class<?> functionClass, int line, int column) Creates a newNaftahBugErrorindicating that the specified function is not supported.- Parameters:
functionName- the name of the unsupported function.functionClass- the class providing the function.line- the line number in the source code where the error occurred.column- the column number in the source code where the error occurred.- Returns:
- a
NaftahBugErrordescribing the unsupported function.
-
newNaftahIllegalArgumentError
public static NaftahBugError newNaftahIllegalArgumentError(String functionName, String providerClassName, int paramCount, int argCount, String functionDetailedString, Exception e, int line, int column) Creates a newNaftahBugErrorfor illegal argument count or mismatched argument types.- Parameters:
functionName- the name of the function being invoked.providerClassName- the class providing the function.paramCount- the expected number of parameters.argCount- the actual number of arguments provided.functionDetailedString- detailed string representation of the function.e- the underlying exception, if any.line- the line number where the error occurred.column- the column number where the error occurred.- Returns:
- a
NaftahBugErrordescribing the illegal argument error.
-
newNaftahInvocationError
public static NaftahBugError newNaftahInvocationError(String functionName, String functionDetailedString, Exception e, int line, int column) Creates a newNaftahBugErrorfor errors occurring during function invocation.- Parameters:
functionName- the name of the function being invoked.functionDetailedString- detailed string representation of the function.e- the underlying exception.line- the line number where the error occurred.column- the column number where the error occurred.- Returns:
- a
NaftahBugErrordescribing the invocation error.
-
newNaftahNonInvocableFunctionError
public static NaftahBugError newNaftahNonInvocableFunctionError(String functionName, String functionDetailedString, int line, int column) Creates a newNaftahBugErrorfor functions that are not invocable.- Parameters:
functionName- the name of the function.functionDetailedString- detailed string representation of the function.line- the line number where the error occurred.column- the column number where the error occurred.- Returns:
- a
NaftahBugErrordescribing the non-invocable function error.
-
newNaftahInstantiationError
public static NaftahBugError newNaftahInstantiationError(String functionName, String functionDetailedString, Exception e, int line, int column) Creates a newNaftahBugErrorwhen object instantiation fails.- Parameters:
functionName- the name of the function triggering instantiation.functionDetailedString- detailed string representation of the function.e- the underlying exception.line- the line number where the error occurred.column- the column number where the error occurred.- Returns:
- a
NaftahBugErrordescribing the instantiation failure.
-
newNaftahInvocableNotFoundError
public static NaftahBugError newNaftahInvocableNotFoundError(String functionName, int line, int column) Creates and throws a newNaftahBugErrorwhen the requested invocable (method or constructor) cannot be found in the current context.The generated error message (in Arabic) indicates that the target callable symbol is undefined or inaccessible.
- Parameters:
functionName- the name of the missing invocable (method or constructor).line- the source line number where the error occurred.column- the source column number where the error occurred.- Returns:
- this method never returns normally; it always throws a
NaftahBugError. - Throws:
NaftahBugError- always thrown to indicate a missing invocable.
-
newNaftahInvocableNotFoundError
Creates a newNaftahBugErrorfor a missing invocable without specifying source line or column.- Parameters:
functionName- the name of the missing invocable (method or constructor)- Returns:
- never returns normally; this method always creates a
NaftahBugError - Throws:
NaftahBugError- always thrown to indicate a missing invocable
-
newNaftahInvocableListFoundError
public static <T extends JvmExecutable> NaftahBugError newNaftahInvocableListFoundError(String functionName, Collection<T> functionsOrClassInitializers, Throwable exception, int line, int column) Creates and throws aNaftahBugErrorwhen a collection of invocable functions or class initializers is found, but the caller did not specify an index to select which one to invoke.This is typically used for overloaded or indexed functions. The generated error message (in Arabic) explains how to select a specific version using a numeric index appended after the function name with a colon.
The message includes a numbered list of all available invocable versions for reference.
- Type Parameters:
T- type ofJvmExecutablein the collection- Parameters:
functionName- the name of the function or class initializerfunctionsOrClassInitializers- a collection of available overloaded or indexed invocablesexception- the original exception that triggered this error, for contextline- the source line number where the error occurredcolumn- the source column number where the error occurred- Returns:
- this method never returns normally; it always throws a
NaftahBugError - Throws:
NaftahBugError- always thrown to indicate ambiguity in the invocable list
-
newNaftahSingleExpressionAssignmentError
Creates and throws aNaftahBugErrorwhen an assignment expression is invalid because it is not provided as a tuple of elements.This error is triggered in situations where the user provides either:
- a single value for assignment instead of a tuple, or
- multiple comma-separated values that are not wrapped in a tuple structure.
The generated message (in Arabic) informs the user that an assignment expression must always be represented as a tuple, regardless of whether the assignment involves a single value or multiple values.
- Parameters:
line- the source line number where the error occurredcolumn- the source column number where the error occurred- Returns:
- this method never returns normally; it always throws
NaftahBugError - Throws:
NaftahBugError- always thrown to indicate an invalid assignment expression
-
newNaftahExpressionsDeclarationsSizeMismatchErrorError
public static NaftahBugError newNaftahExpressionsDeclarationsSizeMismatchErrorError(int line, int column) Creates and throws aNaftahBugErrorwhen the number of assignment expressions does not match the number of declared variables.This typically occurs when performing multiple assignments or tuple-based destructuring, and the left-hand side declarations and right-hand side expressions do not align in count.
The generated message (in Arabic) clarifies that both sides must have an equal number of elements.
- Parameters:
line- the source line number where the error occurredcolumn- the source column number where the error occurred- Returns:
- this method never returns normally; it always throws
NaftahBugError - Throws:
NaftahBugError- always thrown to indicate a mismatch between declarations and expressions
-
newNaftahSpecifiedTypesExceedVariableNamesError
Creates and throws aNaftahBugErrorwhen the parser encounters multiple variable declarations where the number of explicitly specified types exceeds the number of declared variable names.This situation is invalid because each declared variable may have at most one corresponding type, and extra types cannot be mapped to any variable.
The generated message (in Arabic) explains that the number of specified types cannot be greater than the number of variable names.
- Parameters:
line- the source line number where the error occurredcolumn- the source column number where the error occurred- Returns:
- this method never returns normally; it always throws
NaftahBugError - Throws:
NaftahBugError- always thrown to indicate a types-to-variables mismatch
-
newNaftahSettingConstantError
Creates aNaftahBugErrorindicating that an attempt was made to reassign a constant (final or otherwise immutable) variable.This overload simply delegates to
newNaftahSettingConstantError(String, Throwable)with a null cause.The generated error message (written in Arabic) includes the name of the constant that was attempted to be reassigned.
- Parameters:
name- the name of the constant variable being reassigned- Returns:
- a new
NaftahBugErrordescribing the constant reassignment error - Throws:
NaftahBugError- this method always creates an error instance; intended to be thrown by the caller
-
newNaftahSettingConstantError
Creates aNaftahBugErrorindicating that an attempt was made to reassign a constant (final or otherwise immutable) variable, optionally wrapping the underlying cause.If a non-null
Throwableis provided, anIllegalArgumentExceptionwill be created and chained with anIllegalAccessExceptionwhose message mirrors the original cause. This preserves the original error context while signaling that a constant value cannot be modified.The generated error message (in Arabic) includes the name of the constant that was attempted to be reassigned:
"حدث خطأ أثناء إعادة تعيين القيمة الثابتة: '%s'. لا يمكن إعادة تعيين ثابت."
- Parameters:
name- the name of the constant variable being reassignedth- an optional underlying cause; may benull- Returns:
- a new
NaftahBugErrordescribing the constant reassignment error - Throws:
NaftahBugError- this method creates an error instance; intended to be thrown by the caller
-
newIllegalFieldAccessException
Creates anIllegalAccessExceptionindicating that a reflective write to a constant (final/static) field was ignored by the JVM.This can happen when attempting to modify a field that the JVM treats as immutable, such as a final field, where the write is silently ignored without throwing an exception.
- Parameters:
fieldName- the name of the field that was attempted to be modified- Returns:
- an
IllegalAccessExceptiondescribing the failed reflective write
-
newNaftahTypeMismatchError
public static NaftahBugError newNaftahTypeMismatchError(String name, String type, int line, int column) Creates aNaftahBugErrorrepresenting a type-mismatch error with source location information.This overload is used when the type mismatch can be associated with a specific position in the source code (line and column).
The generated error message is localized (Arabic) and indicates that the provided value does not match the expected type.
- Parameters:
name- the name of the value, variable, or parameter that caused the mismatchtype- the expected type description (usually produced by the Naftah type system)line- the source line number where the error occurred, or-1if unknowncolumn- the character position in the line where the error occurred, or-1if unknown- Returns:
- a
NaftahBugErrordescribing the type mismatch
-
newIllegalArgumentException
Creates anIllegalArgumentExceptionindicating that a value has an unsupported runtime type.The generated error message is localized (Arabic) and includes the fully qualified class name of the offending object when available. If the provided object is
null, the message explicitly reportsnullas the unsupported type.This exception does not include source location information and is intended for internal runtime type validation failures.
- Parameters:
object- the object whose runtime type is unsupported; may benull- Returns:
- an
IllegalArgumentExceptiondescribing the unsupported type
-
newIllegalArgumentException
Creates anIllegalArgumentExceptionindicating that a combination of values has unsupported or incompatible runtime types.The generated error message is localized (Arabic) and includes the fully qualified class names of both offending objects when available. If either object is
null,nullis reported explicitly for that position in the message.This overload is typically used when validating operations involving two operands whose runtime types are unsupported or incompatible in combination.
This exception does not include source location information and is intended for internal runtime type validation failures.
- Parameters:
object- the first object with an unsupported runtime type; may benullobject1- the second object with an unsupported runtime type; may benull- Returns:
- an
IllegalArgumentExceptiondescribing the unsupported types
-