Skip to content

A wrapper for try-catch block which spits the result in either ErrorResult or SuccessResult

License

Notifications You must be signed in to change notification settings

varun508/runcatching

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

runcatching

A Node.js module that runs the code in try-catch block and wraps the result in either ErrorResult or SuccessResult

Installation

npm install runcatching --save
yarn add runcatching
bower install runcatching --save

Usage

Javascript

async function login() {
  const result = await AuthService.login(credentials);

  if (result.isFailure()) {
    return next(result.error);
  }
  return result.payload;
}

// AuthService.js
async function login(credentials) {
  return await runCatchingAsync(async () => {
    const loginResponse = await LoginUseCase.loginUser(credentials);
    return loginResponse;
  });
}

TypeScript

async function login(): Promise<Result<LoginResponseDTO>> {
  const result = await AuthService.login(credentials);

  if (result.isFailure()) {
    return next(result.error);
  }
  return result.payload;
}

// AuthService.js
async function login(credentials: LoginRequestDTO): Promise<Result<LoginResponseDTO>> {
  return await runCatchingAsync(async () => {
    const loginResponse = await LoginUseCase.loginUser(credentials);
    return loginResponse;
  });
}

Test

npm run test

About

A wrapper for try-catch block which spits the result in either ErrorResult or SuccessResult

Resources

License

Stars

Watchers

Forks

Packages

No packages published