GithubHelp home page GithubHelp logo

java11-vavr-exception-handling's Introduction

Build Status

java11-vavr-exception-handling

Simple project to show how to handle exceptions using vavr.

preface

Please refer my other github projects:

project description

  1. Suppose we have outer repository (to backup database) that we cannot modify. What is more - find method throws EntityNotFoundException:
    public class LegacyBackupUserRepository {
        User find(int id) {
            return MockBackupDatabase.users.find(User.withId(id))
                    .getOrElseThrow(EntityNotFoundException::new);
        }
    }
    
  2. in our application we have also a repository that:
    • has find method that will lookup into our database
    • has backupLookup method that will call find method from repository mentioned above (LegacyBackupUserRepository)
    • has findOrBackup method that will try to lookup in our database and:
      • if user is found returns it
      • if user is not found - try to lookup in backup database
  3. we want to model our UserRepository in a right way, so:
    • Option is perfect for modelling existence:
      Option<User> find(int id) {
          return MockDatabase.users.find(User.withId(id));
      }
      
    • Try is perfect to cope with third-party libraries that throws exceptions
      Try<User> backupLookup(int id) {
          return Try.of(() -> legacyBackupUserRepository.find(id));
      }
      
    • Either is perfect as a tailored error container
      Either<List<DatabaseUserFindReport>, User> findOrBackup(int id) {
          Option<User> normalUser = find(id);
          if (normalUser.isDefined()) {
              return Either.right(normalUser.get());
          }
          
          Try<User> backupUser = backupLookup(id);
          if (backupUser.isSuccess()) {
              return Either.right(backupUser.get());
          }
          
          return Either.left(List.of(
                  new DatabaseUserFindReport(id, DatabaseType.NORMAL),
                  new DatabaseUserFindReport(id, DatabaseType.BACKUP)
          ));
      
      }
      
      summary: we return user if found otherwise list databases that we looked-up into.

tests

We provide tests for mentioned above functionality.

java11-vavr-exception-handling's People

Contributors

mtumilowicz avatar

Watchers

James Cloos avatar

Forkers

jabrena

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.