Giter VIP home page Giter VIP logo

global_exception_handler's Introduction

Global Exception Handler Java Library for Servlet architecture

Purpose of library

Catch exceptions in the filter and convert it to Global Exception.

Then write response to user about error

It is a global exception handler mechanism which helps user to catch error in ANYWHERE including Controller, Service, Filters, even Spring exceptions are converted.

Usage of Library

<dependency>
    <groupId>com.tgf.exception</groupId>
    <artifactId>handler</artifactId>
    <version>1.0.0</version>
</dependency>

1- First add dependecy to your project. Also enable library with @EnableSingleException that helps to load other related configs.

2- Just select a exception which all exceptions will be converted by simply adding @SelectedException(exception = GlobalException.class)

@EnableSingleException
@SelectedException(exception = GlobalException.class)
@Configuration
public class Config {
}

3- After you enable it. You have to create a custom exception which must implements SingleException.

public class GlobalException extends RuntimeException implements SingleException {

    private JsonNode errorResponse;
    private String message = "";
    
    @Override
    public JsonNode getErrorResponse() {
        return errorResponse;
    }
    @Override
    public void setErrorResponse(JsonNode jsonNode) {
        this.errorResponse = jsonNode;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
}

4- Error response is unstructured data and depend on your implementation as well. Basic response is supported as below response.

{
    "method": "GET",
    "path": "/tgf/data",
    "time": "2024-03-08T01:44:15.4385552",
    "msg": "Request processing failed: com.to.go.fit.exception.GlobalException: Test",
    "status": 500
}

5- If you create a bean which implements ResponseGenerator then you can customize your error response.

@Component
public class CustomGenerator implements ResponseGenerator {
    @Override
    public JsonNode apply(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, JsonNode jsonNode, String message) {
        // you can return Array or object or whatever which will be converted to json.
    }
}

Restrictions

  • Spring context is necessary (Other than spring applications it will not work)
  • Jackson is mandotary.
  • You can check all dependencies in pom.xml which are provided only. You can simply remove provided from them.

global_exception_handler's People

Contributors

grkn avatar

Stargazers

 avatar Can Kaplan avatar

Watchers

 avatar  avatar

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.