Git Product home page Git Product logo

coteafs-error's Introduction

coteafs-error

What is it all about?

This framework helps in handling exceptions gracefully. You can re-throw an exception using message saved in YAML file using corresponding Error code from the file. You can also handle an exception to filter out unwanted stack trace by filtering desired package.

Usage

In your pom.xml, add following block to use this POM,

<dependency>
    <groupId>com.github.wasiqb.coteafs</groupId>
    <artifactId>error</artifactId>
    <version>2.0.0</version>
</dependency>

Error file

An Error file containing Error codes and messages should be available in your src/test/resources folder with name error-codes.yaml. Following is a sample file content,

# src/test/resources/error-codes.yaml
errors:
  404: "File {0} not found!"
  ge: "Generic Error."

Examples

fail method

Use ErrorHandler.fail method to wrap and rethrow the Exception. See example below.

import static com.github.wasiqb.coteafs.error.ErrorHandler.fail;

. . .
// Throw new Custom Error with message.
fail (FileNotFoundException.class, "404", "file.yaml");
. . .

// Throw new Custom Error with message.
try {
    ..some code..
} catch (FileNotFoundException e) {
    fail (GenericError.class, e, "ge");
}
. . .

handleError method

This method helps in collecting and filtering stacktrace for a particular package which can be helpful in logging of errors.

. . .
try {
    [code block]
} catch (final Exception e) {
    // Filter exception to show only exception occurred in only the given package.
    handleError ("com.github.wasiqb", e).forEach (System.err::println);

    // OR

    // This will show complete exception stack occurred.
    handleError (e).forEach (System.err::println);
}
. . .

Will output formatted exception with filtered stacktrace by package name like this,

Error occurred: (com.github.wasiqb.coteafs.error.CoteafsError)
Message: Test Error!
  at com.github.wasiqb.coteafs.error.ErrorHandler: fail (147)
  at com.github.wasiqb.coteafs.error.ErrorHandler: fail (95)
  at com.github.wasiqb.coteafs.error.util.ErrorHandlerTest: testCoteafsErrorWithFiveArg (48)
Caused by: (class java.io.FileNotFoundException)
Message: null
  at com.github.wasiqb.coteafs.error.ErrorHandler: fail (147)
  at com.github.wasiqb.coteafs.error.ErrorHandler: fail (95)
  at com.github.wasiqb.coteafs.error.util.ErrorHandlerTest: testCoteafsErrorWithFiveArg (48)

coteafs-error's People

Contributors

wasiqb avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

coteafs-error's Issues

Move to Java 11.

It is required to move to Java 11 as Sonar cloud is deprecating Java 8 and scanners using them will stop functioning after October 2020.

Upgrade outdated dependencies, fix code analysis

Description of the new feature / enhancement

  • Add CodeQL analysis
  • Update existing workflow to re-enable Sonarcloud analysis.

Scenario when this would be used?

This will help identify code smells and other vulnerabilities more quickly.

Supporting information

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.