This small library holds a set of Exceptions that implements idea of fast, reusable, error codes that can be simple thrown fast in case of unpredictable and unrecoverable application failure.
The idea is to use a set of simple runtime exceptions. They should always take the field Exception ID (Eid) in the making. This field will then be reported when displaying or logging that exception. It can also be viewed on the professional fatal error window of the application as a bug reference. EidRuntimeExceptions contains also additional unique ID to identify each single exception. This approach simplifies the management of exceptions in the application and allows developers to focus on functionalities rather than coming up with the correct statement for the exception.
This approach is best to use with tools and plugins like:
Example:
throw new EidIllegalStateException("20150721:100554", cause);Example log:
pl.wavesoftware.eid.exceptions.EidIllegalStateException: [20150721:100554]<g0qrwx> => Zipfile in invalid format
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
Caused by: java.util.zip.DataFormatException: Zipfile in invalid format
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
... 62 more
This classes shouldn't be used in any public API or library. It is designed to be used for in-house development of end user applications which will report bugs in standardized error pages or post them to issue tracker.
<dependency>
<groupId>pl.wavesoftware</groupId>
<artifactId>eid-exceptions</artifactId>
<version>1.0.0</version>
</dependency>Static convenience methods that help a method or constructor check whether it was invoked correctly (whether its preconditions have been met). These methods generally accept a boolean expression which is expected to be true (or in the case of checkNotNull, an object reference which is expected to be non-null). When false (or null) is passed instead, the EidPreconditions method throws an unchecked exception, which helps the calling method communicate to its caller that that caller has made a mistake.
Each method accepts a EID string or Eid object, which is designed to ease of use and provide strict ID for given exception usage. This approach speed up development of large application and helps support teams by giving both static and random ID for each possible unpredicted bug.
Each example uses static import:
import static pl.wavesoftware.eid.utils.EidPreconditions.*;checkArgument method should be used to check argument of the method, and validate it in technical terms (not business terms).
Example:
// [..]
public static double sqrt(double value);
checkArgument(value >= 0.0, "20150718:012333");
// if ok, calculate the square root
}In this example, checkArgument throws an EidIllegalArgumentException to indicate that developer made an error in its call to sqrt.
checkState method should be used to check state of the class in given moment, and validate it in technical terms (not business terms).
Example:
checkState(a >= 3.14 && b < 0., "20150721:115016");checkNotNull method should be used to check if given non null argument is actually null
Example:
String nonNullUserName = checkNotNull(userName, "20150721:115515");checkElementIndex method can be used to test parameters of an array, before being used
checkElementIndex(index, list.size(), "20150721:115749");Using functional blocks to handle operations, that are intended to operate properly, simplify the code and makes it more readable. It's also good way to deal with untested, uncovered catch blocks. It's easy and gives developers nice way of dealing with countless operations that suppose to work as intended.
Example:
InputStream is = EidPreconditions.tryToExecute(new RiskyCode<InputStream>() {
@Override
public InputStream execute() throws IOException {
return this.getClass().getClassLoader()
.getResourceAsStream("project.properties");
}
}, "20150718:121521");###Contributing
Contributions are welcome!
To contribute, follow the standard git flow of:
- Fork it
- Create your feature branch (
git checkout -b feature/my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature/my-new-feature) - Create new Pull Request
Even if you can't contribute code, if you have an idea for an improvement please open an issue.
- JDK >= 1.6
- 1.0.0
- Support for JDK >= 1.6
- 0.1.0
- initial release
- idea imported from Guava Library and COI code