UNPKG

1.57 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.DefaultSerializer = exports.extendSerializer = void 0;
4function extendSerializer(extend, implementation) {
5 const fallbackDeserializer = extend.deserialize.bind(extend);
6 const fallbackSerializer = extend.serialize.bind(extend);
7 return {
8 deserialize(message) {
9 return implementation.deserialize(message, fallbackDeserializer);
10 },
11 serialize(input) {
12 return implementation.serialize(input, fallbackSerializer);
13 }
14 };
15}
16exports.extendSerializer = extendSerializer;
17const DefaultErrorSerializer = {
18 deserialize(message) {
19 return Object.assign(Error(message.message), {
20 name: message.name,
21 stack: message.stack
22 });
23 },
24 serialize(error) {
25 return {
26 __error_marker: "$$error",
27 message: error.message,
28 name: error.name,
29 stack: error.stack
30 };
31 }
32};
33const isSerializedError = (thing) => thing && typeof thing === "object" && "__error_marker" in thing && thing.__error_marker === "$$error";
34exports.DefaultSerializer = {
35 deserialize(message) {
36 if (isSerializedError(message)) {
37 return DefaultErrorSerializer.deserialize(message);
38 }
39 else {
40 return message;
41 }
42 },
43 serialize(input) {
44 if (input instanceof Error) {
45 return DefaultErrorSerializer.serialize(input);
46 }
47 else {
48 return input;
49 }
50 }
51};