Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-expect

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-expect

Efficient error handling in async-await.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

async-expect

Expectation function taken from expect. This is not for writing test but to use it when writing asynchronus code with the help of async-await. (see below for example use-case)

Usage

Installation

Using npm:

$ npm install --save expect

Then, use as you would anything else:


const expect = require("async-expect")

Use Case

When using async-await it becomes hard to catch errors. We have to wrap each await call to try-catch block to specifically handle that error. Wrapping multiple await calls in one try-catch block will create problem which await call thrown this error.
Example:

Without async-expect

1. One try-catch block
try{ 
    let user = await User.find(ctx.body.username);

    let like = await Post.addLike(user.username);

    let somthingelse = await forSomethingelse();

}catch(err){
    // no way to get find which call returned that error.
    // if those are third party lib function then you'll have rely
    // on regex to find error type and etc
    console.log(err) // nothing you can do at this point.
}
2. Multiple try-catch block
try{
    // let user = await User.find(ctx.body.username);
    var user = await User.find(ctx.body.username);
}catch(err){
    app.redirect("/err", "invalid user")
}

try{
    // problem1 - user isnt accessible here. let block scoped
    // have to user var
    // let like = await Post.addLike(user.username);
    var like = await Post.addLike(user.username);
}catch(err){
    app.redirect("/err", "invalid user")
}

// problem2 - readablity issue

3. Using async-expect

const expect = require("async-expect");

try{ 
    let user = await User.find(ctx.body.username);
    expect(user.username).toExists("Username is invalid", "INVALID-USER")

    let like = await Post.addLike(user.username);
    expect(like.status).toBe(true, 
        "User doesn't have permission to see", "PERM-DENIED")

    let somthingelse = await forSomethingelse();
    expect(somethingelse).toBe(Object, "something else is wrong", "SOMETHING-ERROR", somethingelse)

}catch(err){
    switch(err.name){
        case: "INVALID-USER":
            app.redirect("/register", "Please register to continue");
            break;
        case: "PERM-DENIED":
            app.redirect("/user", "You dont have enough permission");
            break;
        case: "SOMETHING-ERROR":
            let somethingelse = err.extra
            doSomething(somethingelse);
            break;
    }
}

Assertions

Currently it contains 7 assertive function which is more than enough to be useful in conjunction with async-await.

Note: [error-message, error-name, error-extra] parameters are optional.

toExist

expect(object).toExist([error-message, error-name, error-extra])

Asserts the given object is truthy.

expect('something truthy').toExist()

toNotExist

expect(object).toNotExist([error-message, error-name, error-extra])

Asserts the given object is falsy.

expect(null).toNotExist()

toBe

expect(object).toBe(value, [error-message, error-name, error-extra])

Asserts that object is equal to value using ==.

try { 
  let role = await User.getRole();
  expect(role).toBe("admin", "Only admin user is allowed", "NotAdminError");
}catch(err){
  if(err.name == "NotAdminError"){
    app.redirect("/user", {message: "You're not allowed to perform this operation"});
  }
}

toNotBe

expect(object).toNotBe(value, [error-message, error-name, error-extra])

Asserts that object is not equal to value using !=.

toBeA(constructor)

expect(object).toBeA(constructor, [error-message, error-name, error-extra])
expect(object).toBeAn(constructor, [error-message, error-name, error-extra])

Asserts the given object is an instanceof constructor.

expect(new User).toBeA(User)
expect(new Asset).toBeAn(Asset)

toBeA(string)

expect(object).toBeA(string, [error-message, error-name, error-extra])
expect(object).toBeAn(string, [error-message, error-name, error-extra])

Asserts the typeof the given object is string.

expect(2).toBeA('number')

toNotBeA(constructor)

expect(object).toNotBeA(constructor, [error-message, error-name, error-extra])
expect(object).toNotBeAn(constructor, [error-message, error-name, error-extra])

Asserts the given object is not an instanceof constructor.

expect(new Asset).toNotBeA(User)
expect(new User).toNotBeAn(Asset)

toNotBeA(string)

expect(object).toNotBeA(string, [error-message, error-name, error-extra])
expect(object).toNotBeAn(string, [error-message, error-name, error-extra])

Asserts the typeof the given object is not string.

expect('a string').toNotBeA('number')
expect(2).toNotBeAn('object')

Keywords

FAQs

Package last updated on 21 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc