Skip to content

Latest commit

 

History

History
130 lines (107 loc) · 3.93 KB

README.md

File metadata and controls

130 lines (107 loc) · 3.93 KB

Capture4J

Build Status Maintainability codecov License

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.

Getting Started

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.

Prerequisites

Make sure you have installed all of the following prerequisites on your development machine:

Installing

Git

git clone git@github.com:firaja/capture4j.git
cd capture4j
mvn clean install

Maven (requires GitHub Authentication)

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

Profit

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;
    }    
}

Versioning

GitHub release (latest by date) GitHub release (latest by date including pre-releases)

We use SemVer for versioning.

For the versions available, see the tags on this repository.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Authors

  • David Bertoldi - Initial work - firaja

License

This project is licensed under the MIT License - see the LICENSE file for details