stateful-result – nodejs module for represent operation result with status code

Nodejs stateful-result module

This module can be used for representing an operation(e.g: function return) result with a meaningful status code. By default, we suggest to make use of HTTP status code. Hence, code 200 for status of operation OK, and code 404 for status of operation target not found, etc.

Links

Github: https://github.com/airicyu/stateful-result
NPM: https://www.npmjs.com/package/stateful-result

Samples

Sample 1: Getting success result (code 200 OK)

const { Result } = statefulResult.models;

function testSuccess200() {
    return Result.newSuccess({code: 200, data: true});
}

let error, data, code;
[error, data, code] = testSuccess200().get();
console.log([error, data, code]);

console output

[ null, true, 200 ]

Description:

Simple enough, isn’t it?

Sample 2: Getting fail result (code 404 Not found)

const { Result } = statefulResult.models;

function testFail404() {
    return Result.newFail({code: 404});
}

let error, data, code;
[error, data, code] = testFail404().get();
console.log([error, data, code]);

console output

[ { Error
      at Function.newFail (......\stateful-result\src\models\result.js:167:35)
      at testFail404 (......\stateful-result\index.js:14:19)
      at Object.<anonymous> (......\stateful-result\index.js:18:23)
      at Module._compile (module.js:571:32)
      at Object.Module._extensions..js (module.js:580:10)
      at Module.load (module.js:488:32)
      at tryModuleLoad (module.js:447:12)
      at Function.Module._load (module.js:439:3)
      at Module.runMain (module.js:605:10)
      at run (bootstrap_node.js:423:7) name: 'Exception', code: 404, message: 'Not Found' },
  undefined,
  404 ]]

Description: The error object is an extended error object which has the “code” attribute. In this case, the code is 404.

Sample 3: Get result if success result or throw error if returning fail result (code 404 Not found)

const { Result } = statefulResult.models;

function testSuccess200() {
    return Result.newSuccess({code: 200, data: true});
}

function testFail404() {
    return Result.newFail({code: 404});
}

let error, data, code;
try{
    [error, data, code] = testSuccess200().getOrThrow();
    console.log('testSuccess200 result', [error, data, code]);
    [error, data, code] = testFail404().getOrThrow();
} catch(e){
    console.log(e);
}

console output

testSuccess200 result [ null, true, 200 ]
{ Error
    at Function.newFail (......\stateful-result\src\models\result.js:167:35)
    at testFail404 (......\stateful-result\index.js:14:19)
    at Object.<anonymous> (......\stateful-result\index.js:18:23)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:423:7) name: 'Exception', code: 404, message: 'Not Found' }

Description: This time we called method “getOrThrow”. If the result is success, the result would be returned as previous samples. If the result is fail, the error object would be thrown.

Leave a Reply

Your email address will not be published.