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

node-result-error

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-result-error

wrapper for data via error class ResultError

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

node-result-error

wrapper for data via error class ResultError

install

npm install node-result-error

# or
yarn add node-result-error

example

import {ResultOk, ResultFail} from 'node-result';
import {ResultError} from "node-result-error";

function checkParameter(parameters: object, parameterName: string, regExp: RegExp) {
    try {
        const parameterValue = parameters[parameterName];
        if (!regExp.test(parameterValue)) {
            return ResultFail(new ResultError({
                statusCode: 400,
                body: JSON.stringify({
                    error: 'Unsupported parameter value.',
                    parameterName,
                    parameterValue
                })
            }));
        }
        return ResultOk(null);
    } catch (error) {
        return ResultFail(error);
    }
}

((ctx) => {
    try {
        const user = {
            firstName: 'Alex',
            lastName: '_Smith',
            age: '28'
        };
        checkParameter(user, 'firstName', /^[A-Z][a-z]+$/).unwrap();
        checkParameter(user, 'lastName', /^[A-Z][a-z]+$/).unwrap();
        checkParameter(user, 'age', /^[1-9][0-9]?$/).unwrap();
        ctx.status = 200;
        ctx.body = 'ok';
    } catch (error) {
        if (error instanceof ResultError) {
            const data = error.unwrap();
            ctx.status = data.statusCode;
            ctx.body = data.body;
        } else {
            console.error(error);
            ctx.status = 500;
        }
    }
})();

FAQs

Package last updated on 08 Apr 2021

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