-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor and Improve Exception Handling #115
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The `JMErrorCode` enum represents error codes for various error conditions that can occur within a JMatrix library. Each error code is associated with an integer error number, a string representation of the error code, and a descriptive error message.
Added several new constructors to `JMatrixBaseException` class: - JMatrixBaseException(int, String) - JMatrixBaseException(String, Throwable) - JMatrixBaseException(String, Throwable, boolean, boolean) This includes some enhancements and refactors to existing constructors, maintaining their functionality. In addition, private fields to store the stack traces has been removed.
* Defined several private and protected fields used by `printStackTrace` * Introduced a new method called `getErrorCode`, returns the `JMErrorCode` object associated with this throwable * Introduced a new method called `getCause`, returns the causative exception of this throwable, if the cause exception is a reference of this throwable, then returns `null` * Refactored the `toString` and `getMessage` methods to compatible with new features (`JMErrorCode`) * Added a new overload of `printStackTrace` method that accepts a `PrintStream` object * Refactored the `printStackTrace()` method and moved its implementation to `printStackTrace(PrintStream)` method
Changed the error number (errno) of `JMErrorCode.UNKERR` from 200 to 400. Related commit: 789f141
The method will returns a string representation of the corresponding enum with format `<CODE>[<ERRNO>]`. For instance, the `NULLMT` error code will gives `NULLMT[203]`.
Added documentation for the thrown exception (IllegalArgumentException) in the `JMErrorCode.toString()` method.
Refactored several constructors of `JMatrixBaseException` to prevent throwing a NullPointerException due to error code is `null`. In addition to this change, added Javadoc to every undocumented constructors with details description.
* Refactored both the `toString` and `getMessage` methods to prevent from returning a `null` value * Refactored the `printStackTrace` method to make it prints all available stack traces within, this makes developer to read and find the source error easily
Added a new capability to raise an exception, this feature have similar behavior with the existing method `JMatrixUtils.raiseError` but this can be configured by users. To configure, users only need to define an environment variable called "jm.raise". with values as follows: - `auto`, `a`, `yes`, `y` : This will set the internal configuration to `auto`, which means when calling the `JMatrixBaseException.raise` method, it will prints the stack traces of the given throwable and exits with specified (or determined automatically) error number - `manual`, `m`, `no`, `n` : This will set the internal configuration to `manual`, which means when calling the `JMatrixBaseException.raise` method, it will throws back the given throwable and will never exits the application. This behavior supports scenarios where the application or users prefers to handle exceptions using traditional try-catch blocks rather than exiting.
Refactored all `JMatrixBaseException` subclasses to compatible with the new changes in their super class (JMatrixBaseException). This includes, removing unecessary fields and refining several methods. List of classes that being refactored in this change: - InvalidIndexException - IllegalMatrixSizeException - NullMatrixException
Changed the name of auto-raise configuration from "jm.raise" to "jm.autoraise".
The auto-raise configuration can now be set using system properties with `System.setProperty`. A new method, `getRaiseConfigFromSystemProps()`, has been introduced to safely retrieve the auto-raise configuration from system properties. This method catches `SecurityException` during the retrieval process and logs a warning message instead of throwing the exception. Additionally, several methods have been refactored to more effectively retrieve the auto-raise configuration from both environment variables and system properties during runtime, especially `getRaiseConfig` and `isAutoRaise`. NOTE: If the "jm.autoraise" configuration is set using both environment variables and system properties, the system property will take precedence.
Updated the Javadoc of `JMatrixBaseException` by adding description of new feature of capability to configure the behavior of auto-raised exceptions using either the environment variable or system property by specific name. In addition, this change also improved the existing documentation, including the documentation of `toString` method.
In these huge changes of improving every exceptions in the JMatrix library, we have changed the value of `serialVersionUID` field on every non-deprecated exceptions. Now the `serialVersionUID` no longer use the calculation provided in changes #60. Instead, now it uses this format: 430{dd}{MM}{YYYY}{EEE} Where the {dd} is the date of this project was published to GitHub, {MM} is the month of this project was published to GitHub, {YYYY} is the year of this project was published to GitHub, and the {EEE} is the corresponding error code of the exception (you can check the correct error code for each exceptions in `JMErrorCode` enum).
Refactored the exceptions raiser in `Matrix` class by applying the `JMatrixBaseException.raise` method and replacing the `JMatrixUtils.raiseError` method to raise the exception. The `raise` method was implemented with configurable auto-raise exceptions, this offers control over exception handling and developers can choose the most appropriate error-handling strategy based on their specific needs. Related refs: c2f2844
github-actions
bot
added
enhancement
Enhancing existing features
lang:java
Some changes on Java code
labels
Jun 16, 2024
mitsuki31
added
minor
Minor update
feature
Add new features to improve the project
labels
Jun 16, 2024
This test suite contains 7 test cases.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
enhancement
Enhancing existing features
feature
Add new features to improve the project
lang:java
Some changes on Java code
minor
Minor update
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This pull request refactors and improves the exception handling functionality within the JMatrix library. It introduces new capabilities, updates Javadocs, and enhances the existing exception-raising mechanisms. These changes aim to provide more robust error handling, configurable auto-raise behavior, and comprehensive documentation to assist developers in effectively managing exceptions in their applications.
Changes Made
Enum for Error Codes
JMErrorCode
enum to represent error codes within the JMatrix library. Each error code is associated with an integer, a string representation, and a descriptive message.Auto-Raise Configuration
Exception Handling Refactor
Matrix
class to use theJMatrixBaseException.raise
method, replacing the previousJMatrixUtils.raiseError
method. This change provides configurable auto-raise exceptions, allowing developers to choose appropriate error-handling strategies.Serial Version UID Update
serialVersionUID
values for all non-deprecated exceptions to a new format that includes the publication date and error code.JMatrixBaseException
EnhancementsJMatrixBaseException
class to include new features such as configurable auto-raise behavior using environment variables or system properties.toString
andgetMessage
methods to ensure they do not returnnull
.printStackTrace
method to print all available stack traces for easier error source identification.NullPointerException
.Subclass Refactors
JMatrixBaseException
subclasses to align with the new superclass changes, removing unnecessary fields and refining methods for consistency.Documentation Fixes and Updates
Description
This pull request addresses several key areas to improve the robustness and usability of exception handling in the JMatrix library. Here are the detailed descriptions of the improvements:
Enum for Error Codes
Introduced the
JMErrorCode
enum to standardize error codes within the JMatrix library. Each error code in the enum is associated with an integer, a string representation, and a descriptive message. This standardization simplifies error handling and enhances code readability.INVIDX
"Given index is out of bounds"
INVTYP
"Matrix has invalid type of matrix"
NULLMT
"Matrix is null"
UNKERR
"Unknown error"
Exception Improvements and Refactors
Replaced the
JMatrixUtils.raiseError
method with the newJMatrixBaseException.raise
method in theMatrix
class. Theraise
method was introduced to enhance thethrow
statement to more configurable by users and developers, the method will behaves likethrow
keyword when the auto-raise configuration set tomanual
(this can be configured using system property and environment variable), throwing exceptions for traditional try-catch handling. Otherwise, it will prints the stack traces and exit the application immediately which is the default behavior of theraise
method.Added new constructors to handle various exception initialization scenarios, they are:
Improved the
toString
andgetMessage
methods to ensure they do not returnnull
, relying to the exception messages fromJMErrorCode
enum if the error message was not provided ornull
.Refactored the
printStackTrace
method to print all available stack traces, making it easier for developers to trace and identify the source of errors.Introduced new methods such as
getErrorCode
, which returns theJMErrorCode
associated with the exception, andgetCause
, which returns the causative exception.Refactored subclasses such as
InvalidIndexException
,IllegalMatrixSizeException
, andNullMatrixException
to align with the new superclass changes. These refactors removed unnecessary fields, refined methods, and ensured compatibility with the updatedJMatrixBaseException
class.Auto-Raise Configuration
The auto-raise exceptions has been utilized by this library since the
JMatrixUtils.raiseError
method was introduced, this behavior makes any exceptions passed to the method will be printed and then exit the application immediately. This makes it harder for developers to handle the thrown exceptions.These changes here introduced a new feature to configure the auto-raise exceptions behavior both from system property and environment variable. Users and developers can configure the behavior of auto-raise by set either the system property
jm.autoraise
or environment variablejm_autoraise
to specific known values.To deactivate the auto-raise behavior, set the value to one of these known values:
manual
or simplym
no
or simplyn
To activate the auto-raise behavior, set the value to one of these known values:
auto
or simplya
yes
or simplyy
Note
If both the system property
jm.autoraise
and environment variablejm_autoraise
are set, the system property takes precedence. It is RECOMMENDED to configure using system property instead environment variable if possible.The default configuration of auto-raise behavior is
auto
, which you might not have to configure and set the auto-raise configuration toauto
oryes
.Example Usage
Using System Property
From command-line:
During runtime execution (dynamic update):
Using Environment Variable
UNIX:
export jm_autoraise=manual java -cp /path/to/jmatrix.jar Foo.java
Windows:
Warning
You might encountering an
ExceptionInInitializerError
withIllegalArgumentException
as cause exception during runtime execution, this exception was due to passing an unknown value to auto-raise configuration.Deactivating the auto-raise configuration allows developers to handle exceptions using traditional try-catch blocks, providing more control over application flow and error handling.
In addition to this feature addition, within the internal context we also introduced new methods,
getRaiseConfig
andgetRaiseConfigFromSystemProps
, to safely retrieve the auto-raise configuration from environment variables and system properties, respectively. These methods handlesSecurityException
gracefully by logging a warning message instead of throwing an exception.Serial Version UID Update
Updated the
serialVersionUID
values for all non-deprecated exceptions to a new format. The new format430{dd}{MM}{YYYY}{EEE}
includes the publication date and error code, where:{dd}
is the day{MM}
is the month{YYYY}
is the year the project was published on GitHub, and{EEE}
is the error code from theJMErrorCode
enum.This format ensures that each
serialVersionUID
is unique and meaningful.Documentation Improvements
Enhanced the Javadocs for several methods of exception classes to provide detailed descriptions, usage scenarios, cross-references, and detailed exception conditions. This ensures that developers have a comprehensive understanding of the method's functionality and usage.
Summary
This pull request significantly enhances the exception handling capabilities of the JMatrix library. It introduces configurable auto-raise behavior, updates Javadocs for clarity and completeness, and refactors existing exception mechanisms for robustness. These changes provide developers with more control over error handling, improve documentation, and ensure the library's reliability in managing exceptions.
By addressing these key areas, the pull request aims to make the JMatrix library more robust, flexible, and developer-friendly, supporting better error handling practices and improving overall code quality.