This library aims to remove from your code bulky try-catch/if-else blocks and make your code easier to read and maintain.
Write less repetitive checks and enjoy coding efficiently.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
Make sure you have installed all of the following prerequisites on your development machine:
- Java JDK 1.8+ - You can use a JDK from any vendor, but it is recommended to use Oracle JDK or OpenJDK
- Maven 3+ - Download and install
git clone git@github.com:firaja/capture4j.git
cd capture4j
mvn clean install
Insert the following dependency in your pom.xml
<dependency>
<groupId>dev.firaja.utils</groupId>
<artifactId>capture4j</artifactId>
<version>0.1.1</version>
</dependency>
Then run
mvn install
Here a classic example where a method returns the same value in case of error
public int doSomething(MyObject myObject)
{
if(myObject == null || myObject.getId() == null)
{
return -1;
}
String name;
try
{
name = readNameFromSomewhere(myObject.getId());
}
catch (EntryNotFoundException e)
{
return -1;
}
if(name != null && name.indexOf(45) == 'a')
{
return -1;
}
return 1;
}
while the same method can be rewritten with capture4j like this
@EasyCapture(returns = "-1")
public int doSomething(MyObject myObject)
{
String name = readNameFromMyDB(myObject.getId());
return name.indexOf(45) == 'a' ? 1 : -1;
}
You can specify the type of exception you want to treat
@EasyCapture(what = IllegalStateException.class, returns = "illegal")
@EasyCapture(what = NullPointerException.class, returns = "null pointer :(")
public String doSomething()
{
...
}
Or even use a custom handler for standard or custom exceptions
@Capture(what = MyException.class, with = MyHandler.class)
public long doSomething()
{
...
}
class MyHandler implements Handler<Long>
{
public Long handle(Throwable theException) // <- MyException
{
long fallback = doSomeCalculation();
logger.warn("Something went wrong :( but I'returning {}.", fallback, theException);
return fallback;
}
}
We use SemVer for versioning.
For the versions available, see the tags on this repository.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
- David Bertoldi - Initial work - firaja
This project is licensed under the MIT License - see the LICENSE file for details