Socket
Socket
Sign inDemoInstall

defekt

Package Overview
Dependencies
Maintainers
5
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

defekt - npm Package Compare versions

Comparing version 8.1.0 to 8.2.0

build/lib/hydrateCustomError.d.ts

1

build/lib/CustomError.d.ts

@@ -11,3 +11,4 @@ declare class CustomError<TErrorName extends string = string> extends Error {

});
toJSON(): any;
}
export { CustomError };

@@ -11,3 +11,27 @@ "use strict";

}
// eslint-disable-next-line @typescript-eslint/naming-convention
toJSON() {
const serializableObject = {
name: this.name,
message: this.message,
code: this.code,
stack: this.stack
};
try {
JSON.stringify(this.data);
serializableObject.data = this.data;
}
catch {
// If data is not serializable, we want to omit it from the returned object.
}
try {
JSON.stringify(this.cause);
serializableObject.cause = this.cause;
}
catch {
// If data is not serializable, we want to omit it from the returned object.
}
return serializableObject;
}
}
exports.CustomError = CustomError;

4

build/lib/index.d.ts
import { CustomError } from './CustomError';
import { CustomErrorConstructor } from './CustomErrorConstructor';
import { defekt } from './defekt';
import { hydrateResult } from './hydrateResult';
import { isCustomError } from './isCustomError';

@@ -8,3 +9,4 @@ import { isError } from './isError';

import { error, Result, ResultDoesNotContainError, value } from './Result';
export { CustomError, defekt, error, isCustomError, isError, isResult, ResultDoesNotContainError, value };
import { hydrateCustomError, HydratingErrorFailed } from './hydrateCustomError';
export { CustomError, defekt, error, hydrateCustomError, HydratingErrorFailed, hydrateResult, isCustomError, isError, isResult, ResultDoesNotContainError, value };
export type { CustomErrorConstructor, Result };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.value = exports.ResultDoesNotContainError = exports.isResult = exports.isError = exports.isCustomError = exports.error = exports.defekt = exports.CustomError = void 0;
exports.value = exports.ResultDoesNotContainError = exports.isResult = exports.isError = exports.isCustomError = exports.hydrateResult = exports.HydratingErrorFailed = exports.hydrateCustomError = exports.error = exports.defekt = exports.CustomError = void 0;
const CustomError_1 = require("./CustomError");

@@ -8,2 +8,4 @@ Object.defineProperty(exports, "CustomError", { enumerable: true, get: function () { return CustomError_1.CustomError; } });

Object.defineProperty(exports, "defekt", { enumerable: true, get: function () { return defekt_1.defekt; } });
const hydrateResult_1 = require("./hydrateResult");
Object.defineProperty(exports, "hydrateResult", { enumerable: true, get: function () { return hydrateResult_1.hydrateResult; } });
const isCustomError_1 = require("./isCustomError");

@@ -19,1 +21,4 @@ Object.defineProperty(exports, "isCustomError", { enumerable: true, get: function () { return isCustomError_1.isCustomError; } });

Object.defineProperty(exports, "value", { enumerable: true, get: function () { return Result_1.value; } });
const hydrateCustomError_1 = require("./hydrateCustomError");
Object.defineProperty(exports, "hydrateCustomError", { enumerable: true, get: function () { return hydrateCustomError_1.hydrateCustomError; } });
Object.defineProperty(exports, "HydratingErrorFailed", { enumerable: true, get: function () { return hydrateCustomError_1.HydratingErrorFailed; } });

@@ -0,1 +1,8 @@

# [8.2.0](https://github.com/thenativeweb/defekt/compare/8.1.0...8.2.0) (2022-02-22)
### Features
* Hydrate CustomError and Result. ([#344](https://github.com/thenativeweb/defekt/issues/344)) ([3f4343c](https://github.com/thenativeweb/defekt/commit/3f4343c06ebceea735f5f086768cdfc5c2e04340))
# [8.1.0](https://github.com/thenativeweb/defekt/compare/8.0.0...8.1.0) (2022-01-17)

@@ -2,0 +9,0 @@

@@ -20,2 +20,27 @@ class CustomError<TErrorName extends string = string> extends Error {

}
// eslint-disable-next-line @typescript-eslint/naming-convention
public toJSON (): any {
const serializableObject: any = {
name: this.name,
message: this.message,
code: this.code,
stack: this.stack
};
try {
JSON.stringify(this.data);
serializableObject.data = this.data;
} catch {
// If data is not serializable, we want to omit it from the returned object.
}
try {
JSON.stringify(this.cause);
serializableObject.cause = this.cause;
} catch {
// If data is not serializable, we want to omit it from the returned object.
}
return serializableObject;
}
}

@@ -22,0 +47,0 @@

import { CustomError } from './CustomError';
import { CustomErrorConstructor } from './CustomErrorConstructor';
import { defekt } from './defekt';
import { hydrateResult } from './hydrateResult';
import { isCustomError } from './isCustomError';

@@ -8,2 +9,3 @@ import { isError } from './isError';

import { error, Result, ResultDoesNotContainError, value } from './Result';
import { hydrateCustomError, HydratingErrorFailed } from './hydrateCustomError';

@@ -14,2 +16,5 @@ export {

error,
hydrateCustomError,
HydratingErrorFailed,
hydrateResult,
isCustomError,

@@ -16,0 +21,0 @@ isError,

{
"name": "defekt",
"version": "8.1.0",
"version": "8.2.0",
"description": "defekt is custom errors made simple.",

@@ -36,5 +36,5 @@ "contributors": [

"devDependencies": {
"assertthat": "6.3.14",
"roboter": "12.6.1",
"semantic-release-configuration": "2.0.5"
"assertthat": "6.4.0",
"roboter": "12.7.0",
"semantic-release-configuration": "2.0.7"
},

@@ -41,0 +41,0 @@ "scripts": {},

@@ -120,2 +120,23 @@ # defekt

#### Serializing, Deserializing and Hydrating errors
Sometimes you need to serialize and deserialize your errors. Afterwards they are missing their prototype-chain and `Error`-related functionality. To restore those, you can hydrate a raw object to a `CustomError`-instance:
```typescript
import { defekt, hydrateCustomError } from 'defekt';
class TokenMalformed extends defekt({ code: 'TokenMalformed' }) {}
const serializedTokenMalformedError = JSON.stringify(new TokenMalformed());
const rawEx = JSON.parse(serializedTokenMalformedError);
const ex = hydrateCustomError({ rawEx, potentialErrorConstructors: [ TokenMalformed ] }).unwrapOrThrow();
```
Note that the hydrated error is wrapped in a `Result`. If the raw error can not be hydrated using one of the given potential error constructors, an error-`Result` will be returned, which tells you, why the hydration was unsuccessful.
Also note that the `cause` of a `CustomError` is currently not hydrated, but left as-is.
Usually, JavaScript `Error`s are not well suited for JSON-serialization. To improve this, the `CustomError` class implements [`toJSON()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior), which defines custom JSON-serialization behavior. If you want to be able to serialize your `cause` and `data` as well, they need to be either plain objects or also implement the `toJSON` method.
### Using custom error type-guards

@@ -290,2 +311,26 @@

### Hydrating a `Result`
Like for errors, there is a function to hydrate a `Result` from raw data in case you need to serialize and deserialize a `Result`.
```typescript
import { defekt, hydrateResult } from 'defekt';
const rawResult = JSON.parse(resultFromSomewhere);
const hydrationResult = hydrateResult({ rawResult });
if (hydrationResult.hasError()) {
// The hydration has failed.
} else {
const result = hydrationResult.value;
if (result.hasError()) {
// Continue with your normal error handling.
}
}
```
You can also optionally let `hydrateResult` hydrate the contained error by passing `potentialErrorConstructors`. This works identically to `hydrateResult`.
## Running quality assurance

@@ -292,0 +337,0 @@

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