Coder Social home page Coder Social logo

feign-error-decoder's Introduction

Build Status MIT license Maven Central

Feign Reflection ErrorDecoder

This small library implements ErrorDecoder to provide a simple way to map a key returned on an API to a specific exception declared thrown on the client interface.

It's very useful in an application with many microservices calling each others when you want to handle specific checked or runtime exceptions on the client side.

Blog post explaining more about the rationale of this library.

Maven

The plugin is available on Maven Central :

    <dependency>
      <groupId>com.coveo</groupId>
      <artifactId>feign-error-decoder</artifactId>
      <version>1.3.0</version>
    </dependency>

Usage

A complete example is shown in this repo as the base setup for the unit tests.

Requirements

Base Exception class

In order to use this library, you need a base exception which all the exceptions declared thrown on the client interface will inherit from. This exception needs to provide a way to access a unique String key per subclass.

public abstract class ServiceException extends Exception {
  private String errorCode;
  //Constructors omitted
  
  public String getErrorCode() {
    return errorCode;
  }
}

Class for the error response

A Class representing the body of the response sent on the API in case of error is also needed. This is the Class that will be instantiated by Feign Decoder. It needs a method to get the key that will be used to get the correct exception. It can also optionally have a message that will be injected in the detailMessage field of Throwable.

public class ErrorCodeAndMessage {
  private String message;
  private String errorCode;
  //getters and setters omitted

How to use

With these requirements met, all left to do is to extend ReflectionErrorDecoder and implement the abstract methods required like this :

import feign.Response;
import feign.codec.ErrorDecoder;

public class ServiceExceptionErrorDecoder
    extends ReflectionErrorDecoder<ErrorCodeAndMessage, ServiceException> {

  public ServiceExceptionErrorDecoder(Class<?> apiClass) {
    super(apiClass, ErrorCodeAndMessage.class, ServiceException.class);
  }

  @Override
  protected String getKeyFromException(ServiceException exception) {
    return exception.getErrorCode();
  }

  @Override
  protected String getKeyFromResponse(ErrorCodeAndMessage apiResponse) {
    return apiResponse.getErrorCode();
  }

  @Override
  protected String getMessageFromResponse(ErrorCodeAndMessage apiResponse) {
    return apiResponse.getMessage();
  }
}

Use the Feign builder to inject the ErrorDecoder in your client.

Supported interface annotations

The supported annotations on the interfaces are Feign's @RequestLine and Spring @RequestMapping. As of version 1.2.0, it also supports @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping.

Optional customization

Exception inheritance support with classpath scanning

A ClassHierarchySupplier interface is used to support classpath scanning to fetch the hierarchy of abstract exception classes. This allows you to declare a specific base exception as thrown on the client interface and let the interface scan all the possible exceptions that can be thrown.

With Spring

An optional dependency on Spring Context is included in the library to enable this. All you need to do is have Spring framework available in your project and the proper implementation will be instantiated. By default, it will scan the exception children in all packages. To restrict the base package to be scanned, simply use the constructor with the basePackage field.

Without Spring

A default implementation is not provided at the moment. Feel free to submit a PR if you implement it!

Custom Decoder

By default, this project uses the JacksonDecoder implementation of Feign Decoder interface. A protected setter is available to use your own Decoder.

Custom fallback ErrorDecoder

ErrorDecoder.Default is used by default when no exception is found in the scanned exceptions. A protected setter is available to use your own fallback ErrorDecoder.

Supported constructor arguments

The library has a default list of supported argument types for the exception constructors. It supports empty and constructors with any number of String or Throwable in any order. To extend supported exception types, just override the method protected List<Object> getSupportedConstructorArgumentInstances(). Just make sure to return the default types of String and Throwable if you still want them to be supported.

Contributing

PR are always welcome and please open an issue if you find any bugs or wish to request an additional feature.

feign-error-decoder's People

Contributors

jebeaudet avatar arnoldtempfli avatar

Watchers

sharmistha 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.