6.5.15 • Published 1 month ago

@softvisio/result v6.5.15

Weekly downloads
-
License
ISC
Repository
github
Last release
1 month ago

:information_source: Please, see the full project documentation here:https://softvisio-node.github.io/result/

Introduction

Serializable result object, that encapsulates operation status, statusText and returned data.

Install

npm install @softvisio/result

Under node environment it also register itself as global.result, so you don;t need to import it into all modules;

Usage

import result from "@softvisio/result";

var res = result( 200 );

res = result( [ 200, "Completed" ] ); // custom status text

res = result( 200, { data } ); // with some data

try {
    res = result.try( await someFunctionCall() );
}
catch ( e ) {
    res = result.catch( e );
}

result( status, data?, meta? )

  • status {integer|Array|Error} Status code, {Error} object, result-like object (has status and statusText properties, for example, node-fetch result) or {Array}:
    • {integer} Status code.
    • {string} Status text.
  • data? {any} arbitrary Data that represents returned value. This data is accesible via result.data property.
  • meta? {Object} Additional meta properties, that will be stored in the result object.
  • Returns: {Result}.

Creates new result object.

Example:

const res = result( 500, { "a": 1 }, { "meta": "some data" } );

console.log( res.ok ); // false
console.log( res.status ); // 500
console.log( res.statustext ); // internal server error
console.log( res.data ); // {"a": 1}
console.log( res.meta ); // "some data"

result.exception( status, data?, meta? )

  • status {integer|Array} Status code or {Array} [status code, status text].
  • data? {any} arbitrary Data that represents returned value. This data is accesible via result.data property.
  • meta? {Object} Additional meta properties, that will be stored in the result object.
  • Returns: {Result}.

Creates new result exception object. Same, as result( status, data, properties ), but also set result.exception to the true in case if result is not successuful.

result.try( res?, options? )

  • res? {any} Value to check.
  • options? {Object}:
    • allowUndefined {boolean} Returns error if set to false. Default: false.
    • keepError {boolean} If res is instance of the {Error} set error message as result statusText.
  • Returns: {Result}.

Checks, that res is instance of {Result}. If res is:

  • {undefined}: returns result( 200 ).
  • {Result}: returns res as is.
  • Result-like object (object, that has status and statusText properties): returns result( [res.status, res.statusText] ).
  • {Error}: returns result( [500, error.message] ).
  • Any other value: returns result( 500 ).

result.catch( res, options? )

  • res {any} Value to check.
  • options? {Object}:
    • keepError {boolean} If res is instance of {Error} set error message as result statusText.
    • log {boolean} Do not print error to the console.
  • Returns: {Result}.

Checks, that res is instance of {Result}. If res is:

  • {Result}: returns res as is;
  • Result-like object (object, that has status and statusText properties): returns result( [res.status, res.statusText] ).
  • {Error}: returns result( [500, error.message] ).
  • Any other value converted to the {Error} object (Error( res )) and processed as described above.

result.fromJson( res )

  • res {Object} Result object data, produced by Result.toJSON().
  • Returns: {Result}.

result.fromJsonRpc)

  • msg {Object} JSON RPC 2.0 message.
  • Returns: {Result}.

Converts JSON RPC response message to the {Result} object.

result.getHttpStatus( status )

  • status {integer} Status code.
  • Returns: {integer} Status code.

Returns status code in range, supported by HTTP protocol.

result.getStatusText( status )

  • status {integer} Status code.
  • Returns: {string} Status text.

Resolves status text by the status code.

Class: Result

result.status

  • {integer} Status code.

result.statusText

  • {string} Status text.

result.isException

  • {boolean} Returns true if result is exception.

result.ok

  • {boolean} Returns true if result is successful, otherwise returns false.

result.error

  • {boolean} Returns true if result is error.

result.is1xx

  • {boolean} Returns true if result code is in range: 100 - 199.

result.is2xx

  • {boolean} Returns true if result code is in range: 200 - 299.

result.is3xx

  • {boolean} Returns true if result code is in range: 300 - 399.

result.is4xx

  • {boolean} Returns true if result code is in range: 400 - 499.

result.is5xx

  • {boolean} Returns true if result code >= 500.

result.toString()

  • Returns: {string}.

result.toJSON()

  • Returns: {string}.

result.toJsonRpc( id )

  • id {integer} JSON RPC message id.
  • Returns: {Object} JSON RPC response.

Converts result object to the JSON RPC response.

Status codes

RPC status codes

RPC request errors

Status CodeStatus Text
-32800RPC calls are not supported
-32802Too many requests
-32803Unsupported content type
-32804HTTP method not allowed
-32807Unable to decode RPC message body
-32808Parameters validation error
-32809API method not found
-32810Persistent connection is required to call API method
-32811Access denied
-32812Authorization is required
-32813Session is disabled
-32814Backend is down
-32815Session was deleted
-32816Service is shutting down
-32817Request aborted

HTTP status codes

Informational

Status CodeStatus Text
100Continue
101Switching Protocols
102Processing
103Early Hints

Success

Status CodeStatus Text
200OK
201Created
202Accepted
203Non-Authoritative Information
204No Content
205Reset Content
206Partial Content
207Multi-Status
208Already Reported
226IM Used

Redirect

Status CodeStatus Text
300Multiple Choices
301Moved Permanently
302Found
303See Other
304Not Modified
305Use Proxy
307Temporary Redirect
308Permanent Redirect

Client error

Status CodeStatus Text
400Bad Request
401Unauthorized
402Payment Required
403Forbidden
404Not Found
405Method Not Allowed
406Not Acceptable
407Proxy Authentication Required
408Request Timeout
409Conflict
410Gone
411Length Required
412Precondition Failed
413Payload Too Large
414URI Too Long
415Unsupported Media Type
416Range Not Satisfiable
417Expectation Failed
418I'm a Teapot
421Misdirected Request
422Unprocessable Entity
423Locked
424Failed Dependency
425Too Early
426Upgrade Required
428Precondition Required
429Too Many Requests
431Request Header Fields Too Large
451Unavailable For Legal Reasons

Server error

Status CodeStatus Text
500Internal Server Error
501Not Implemented
502Bad Gateway
503Service Unavailable
504Gateway Timeout
505HTTP Version Not Supported
506Variant Also Negotiates
507Insufficient Storage
508Loop Detected
509Bandwidth Limit Exceeded
510Not Extended
511Network Authentication Required

WebSockets status codes

Status CodeStatus Text
1000Normal Closure
1001Going Away
1002Protocol error
1003Unsupported Data
1004Reserved
1005No Status Rcvd
1006Abnormal Closure
1007Invalid Payload
1008Policy Violation
1009Message Too Big
1010Mandatory Extension
1011Internal Error
1012Service Restart
1013Try Again Later
1014Bad Gateway
1015TLS Handshake
6.5.11

5 months ago

6.5.10

5 months ago

6.5.13

4 months ago

6.5.12

5 months ago

6.5.15

1 month ago

6.5.14

1 month ago

6.5.8

8 months ago

6.5.7

8 months ago

6.5.9

7 months ago

6.5.4

9 months ago

6.5.6

9 months ago

6.5.5

9 months ago

6.5.3

9 months ago

6.5.2

9 months ago

6.5.1

9 months ago

6.5.0

9 months ago

6.3.25

9 months ago

6.3.23

9 months ago

6.3.24

9 months ago

6.4.0

9 months ago

6.3.18

11 months ago

6.3.19

11 months ago

6.3.16

12 months ago

6.3.17

11 months ago

6.3.10

1 year ago

6.3.11

12 months ago

6.3.14

12 months ago

6.3.15

12 months ago

6.3.12

12 months ago

6.3.13

12 months ago

6.3.21

10 months ago

6.3.22

10 months ago

6.3.20

10 months ago

6.3.8

1 year ago

6.3.9

1 year ago

6.3.6

1 year ago

6.3.7

1 year ago

6.3.5

1 year ago

6.3.4

1 year ago

6.3.0

2 years ago

6.1.2

2 years ago

6.1.1

2 years ago

6.3.2

2 years ago

6.1.4

2 years ago

6.3.1

2 years ago

6.1.3

2 years ago

6.2.0

2 years ago

6.3.3

2 years ago

6.1.5

2 years ago

6.1.0

2 years ago

6.0.3

2 years ago

6.0.5

2 years ago

6.0.4

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.2

2 years ago

5.7.0

2 years ago

5.6.11

2 years ago

5.6.10

2 years ago

5.6.9

3 years ago

5.6.8

3 years ago

5.6.7

3 years ago

5.6.6

3 years ago

5.6.5

3 years ago

5.6.4

3 years ago

5.6.3

3 years ago

5.6.2

3 years ago

5.6.1

3 years ago

5.6.0

3 years ago

5.5.0

3 years ago

5.3.3

3 years ago

5.3.2

4 years ago

5.4.6

3 years ago

5.4.5

3 years ago

5.4.4

3 years ago

5.4.3

3 years ago

5.4.2

3 years ago

5.4.1

3 years ago

5.4.0

3 years ago

5.3.1

4 years ago

5.3.0

4 years ago

5.2.2

4 years ago

5.2.1

4 years ago

5.2.0

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.5.4

4 years ago

4.5.3

4 years ago

4.5.2

4 years ago

4.5.0

4 years ago

4.5.1

4 years ago

4.4.0

4 years ago

4.3.0

4 years ago

4.1.7

4 years ago

4.2.0

4 years ago

4.1.6

4 years ago

4.1.4

4 years ago

4.1.3

4 years ago

4.1.5

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

2.0.0-alpha.1

4 years ago

2.0.0-alpha.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago