Socket
Socket
Sign inDemoInstall

@syncot/error

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@syncot/error - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

11

CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

# [0.3.0](https://github.com/SyncOT/SyncOT/compare/@syncot/error@0.2.1...@syncot/error@0.3.0) (2020-11-10)
### Features
* add isError, isCustomError, toJSON and fromJSON ([b38c386](https://github.com/SyncOT/SyncOT/commit/b38c386f5bc96a78f3db9410f8cf1038ae7f8ec5))
## [0.2.1](https://github.com/SyncOT/SyncOT/compare/@syncot/error@0.2.0...@syncot/error@0.2.1) (2020-11-03)

@@ -8,0 +19,0 @@

31

lib/error.d.ts

@@ -1,16 +0,31 @@

export interface ErrorDetails {
name?: string;
message?: string;
cause?: Error;
[key: string]: any;
}
/**
* Extends Error with:
* - the optional `cause` property which indicates the cause of this error,
* - any additional custom properties to provide more details about the error.
*/
export interface CustomError extends Error {
cause?: Error;
cause?: CustomError;
[key: string]: any;
}
/**
* Determines if `data` is an Error.
*/
export declare function isError(data: any): data is Error;
/**
* Determines if `data` is a CustomError.
*/
export declare function isCustomError(data: any): data is CustomError;
/**
* Converts `error` to a JSON object.
*/
export declare function toJSON(error: CustomError): CustomError;
/**
* Converts `error` to a CustomError instance created using the Error constructor.
*/
export declare function fromJSON(error: CustomError): CustomError;
/**
* Creates a new Error instance with the specified properties.
* Additionally, `cause.message` is automatically appended to the new error's `message`.
*/
export declare function createError(details?: ErrorDetails): CustomError;
export declare function createError(data?: Partial<CustomError>): CustomError;
/**

@@ -17,0 +32,0 @@ * A SyncOtError is a CustomError with the `name` matching the regex /^SyncOtError($| )/.

@@ -1,22 +0,75 @@

function assert(value, message) {
if (!value) {
throw createAssertError(message);
/**
* Determines if `data` is an Error.
*/
export function isError(data) {
return (data !== null &&
typeof data === 'object' &&
typeof data.name === 'string' &&
typeof data.message === 'string');
}
/**
* Determines if `data` is a CustomError.
*/
export function isCustomError(data) {
return (isError(data) &&
(data.cause == null || isCustomError(data.cause)));
}
/**
* Converts `error` to a JSON object.
*/
export function toJSON(error) {
if (!isError(error)) {
return {
name: 'TypeError',
message: 'Invalid "error" object.',
error,
};
}
const result = {
...error,
name: error.name,
message: error.message,
};
if (error.cause != null) {
result.cause = toJSON(error.cause);
}
return result;
}
/**
* Converts `error` to a CustomError instance created using the Error constructor.
*/
export function fromJSON(error) {
if (!isError(error)) {
return createError({
name: 'TypeError',
message: 'Invalid "error" object.',
error,
});
}
const result = createError({
...error,
name: error.name,
message: error.message,
cause: undefined,
});
if (error.cause != null) {
result.cause = fromJSON(error.cause);
}
return result;
}
/**
* Creates a new Error instance with the specified properties.
* Additionally, `cause.message` is automatically appended to the new error's `message`.
*/
export function createError(details = {}) {
assert(details != null && typeof details === 'object', 'Argument "details" must be an object.');
assert(typeof details.name === 'string' || details.name === undefined, 'Argument "details.name" must be a string or undefined.');
assert(typeof details.message === 'string' || details.message === undefined, 'Argument "details.message" must be a string or undefined.');
assert(details.cause instanceof Error || details.cause === undefined, 'Argument "details.cause" must be an Error or undefined.');
assert(!('stack' in details), 'Argument "details.stack" must not be present.');
const name = details.name;
const cause = details.cause;
const message = details.message
export function createError(data = {}) {
assert(data != null && typeof data === 'object', 'Argument "data" must be an object.');
assert(typeof data.name === 'string' || data.name === undefined, 'Argument "data.name" must be a string or undefined.');
assert(typeof data.message === 'string' || data.message === undefined, 'Argument "data.message" must be a string or undefined.');
assert(isError(data.cause) || data.cause === undefined, 'Argument "data.cause" must be an Error or undefined.');
const name = data.name;
const cause = data.cause;
const message = data.message
? cause
? `${details.message} => ${cause}`
: details.message
? `${data.message} => ${cause}`
: data.message
: cause

@@ -33,7 +86,5 @@ ? `=> ${cause}`

}
for (const key in details) {
if (details.hasOwnProperty(key) &&
key !== 'name' &&
key !== 'message') {
error[key] = details[key];
for (const key in data) {
if (data.hasOwnProperty(key) && key !== 'name' && key !== 'message') {
error[key] = data[key];
}

@@ -44,3 +95,3 @@ }

export function isSyncOtError(error) {
return error instanceof Error && /^SyncOtError($| )/.test(error.name);
return isCustomError(error) && /^SyncOtError($| )/.test(error.name);
}

@@ -54,3 +105,3 @@ export function createTsonError(message) {

export function isTsonError(error) {
return error instanceof Error && error.name === 'SyncOtError TSON';
return isCustomError(error) && error.name === 'SyncOtError TSON';
}

@@ -77,3 +128,3 @@ /**

export function isInvalidEntityError(error) {
return error instanceof Error && error.name === 'SyncOtError InvalidEntity';
return isCustomError(error) && error.name === 'SyncOtError InvalidEntity';
}

@@ -89,3 +140,3 @@ export function createTypeNotFoundError(typeName) {

export function isTypeNotFoundError(error) {
return error instanceof Error && error.name === 'SyncOtError TypeNotFound';
return isCustomError(error) && error.name === 'SyncOtError TypeNotFound';
}

@@ -99,3 +150,3 @@ export function createNoServiceError(message) {

export function isNoServiceError(error) {
return error instanceof Error && error.name === 'SyncOtError NoService';
return isCustomError(error) && error.name === 'SyncOtError NoService';
}

@@ -109,3 +160,3 @@ export function createDisconnectedError(message) {

export function isDisconnectedError(error) {
return error instanceof Error && error.name === 'SyncOtError Disconnected';
return isCustomError(error) && error.name === 'SyncOtError Disconnected';
}

@@ -119,3 +170,3 @@ export function createNotInitializedError(message) {

export function isNotInitializedError(error) {
return error instanceof Error && error.name === 'SyncOtError NotInitialized';
return isCustomError(error) && error.name === 'SyncOtError NotInitialized';
}

@@ -129,4 +180,3 @@ export function createAlreadyInitializedError(message) {

export function isAlreadyInitializedError(error) {
return (error instanceof Error &&
error.name === 'SyncOtError AlreadyInitialized');
return (isCustomError(error) && error.name === 'SyncOtError AlreadyInitialized');
}

@@ -140,4 +190,3 @@ export function createUnexpectedSessionIdError(message) {

export function isUnexpectedSessionIdError(error) {
return (error instanceof Error &&
error.name === 'SyncOtError UnexpectedSessionId');
return (isCustomError(error) && error.name === 'SyncOtError UnexpectedSessionId');
}

@@ -151,3 +200,3 @@ export function createUnexpectedVersionNumberError(message) {

export function isUnexpectedVersionNumberError(error) {
return (error instanceof Error &&
return (isCustomError(error) &&
error.name === 'SyncOtError UnexpectedVersionNumber');

@@ -162,3 +211,3 @@ }

export function isUnexpectedSequenceNumberError(error) {
return (error instanceof Error &&
return (isCustomError(error) &&
error.name === 'SyncOtError UnexpectedSequenceNumber');

@@ -174,3 +223,3 @@ }

export function isSessionError(error) {
return error instanceof Error && error.name === 'SyncOtError Session';
return isCustomError(error) && error.name === 'SyncOtError Session';
}

@@ -185,3 +234,3 @@ export function createPresenceError(message, cause) {

export function isPresenceError(error) {
return error instanceof Error && error.name === 'SyncOtError Presence';
return isCustomError(error) && error.name === 'SyncOtError Presence';
}

@@ -196,3 +245,3 @@ export function createAuthError(message, cause) {

export function isAuthError(error) {
return error instanceof Error && error.name === 'SyncOtError Auth';
return isCustomError(error) && error.name === 'SyncOtError Auth';
}

@@ -206,3 +255,3 @@ export function createDuplicateIdError(message) {

export function isDuplicateIdError(error) {
return error instanceof Error && error.name === 'SyncOtError DuplicateId';
return isCustomError(error) && error.name === 'SyncOtError DuplicateId';
}

@@ -216,3 +265,3 @@ export function createInvalidStreamError(message) {

export function isInvalidStreamError(error) {
return error instanceof Error && error.name === 'SyncOtError InvalidStream';
return isCustomError(error) && error.name === 'SyncOtError InvalidStream';
}

@@ -227,3 +276,3 @@ export function createSocketError(message, cause) {

export function isSocketError(error) {
return error instanceof Error && error.name === 'SyncOtError Socket';
return isCustomError(error) && error.name === 'SyncOtError Socket';
}

@@ -238,3 +287,3 @@ export function createCompositeError(message, errors = []) {

export function isCompositeError(error) {
return error instanceof Error && error.name === 'SyncOtError Composite';
return isCustomError(error) && error.name === 'SyncOtError Composite';
}

@@ -248,3 +297,3 @@ export function createAssertError(message) {

export function isAssertError(error) {
return error instanceof Error && error.name === 'SyncOtError Assert';
return isCustomError(error) && error.name === 'SyncOtError Assert';
}

@@ -259,3 +308,8 @@ export function createPingError(message, cause) {

export function isPingError(error) {
return error instanceof Error && error.name === 'SyncOtError Ping';
return isCustomError(error) && error.name === 'SyncOtError Ping';
}
function assert(value, message) {
if (!value) {
throw createAssertError(message);
}
}
{
"name": "@syncot/error",
"version": "0.2.1",
"version": "0.3.0",
"description": "Utilities for managing errors.",

@@ -31,3 +31,3 @@ "keywords": [

},
"gitHead": "2399fa565e50dcfc2f4d37afd567c6cbcfdccb15"
"gitHead": "5ba3e377863fb1d00a2c1fb556ae6832f9950ada"
}
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