Comparing version 0.0.2 to 0.0.3
declare function addContextToErr(err: string | null, context: string): string | null; | ||
declare function composeErr(...inputs: any): string; | ||
declare function composeErr(...inputs: any): string | null; | ||
export { addContextToErr, composeErr }; |
@@ -0,1 +1,2 @@ | ||
import { tryStringify } from "./stringify.js"; | ||
// addContextToErr is a helper function that standardizes the formatting of | ||
@@ -7,2 +8,6 @@ // adding context to an error. Within the world of go we discovered that being | ||
// addContextToErr will return null if the input err is null. | ||
// | ||
// NOTE: To protect against accidental situations where an Error type or some | ||
// other type is provided instead of a string, we wrap both of the inputs with | ||
// tryStringify before returning them. This prevents runtime failures. | ||
function addContextToErr(err, context) { | ||
@@ -12,3 +17,3 @@ if (err === null) { | ||
} | ||
return context + ": " + err; | ||
return tryStringify(context) + ": " + tryStringify(err); | ||
} | ||
@@ -23,24 +28,18 @@ // composeErr takes a series of inputs and composes them into a single string. | ||
let result = ""; | ||
let resultEmpty = true; | ||
for (let i = 0; i < inputs.length; i++) { | ||
// Prepend a newline if this isn't the first element. | ||
if (i !== 0) { | ||
result += "\n"; | ||
} | ||
// Strings can be added without modification. | ||
if (typeof inputs[i] === "string") { | ||
result += inputs[i]; | ||
if (inputs[i] === null) { | ||
continue; | ||
} | ||
// Everything else needs to be stringified, log an error if it | ||
// fails. | ||
try { | ||
let str = JSON.stringify(inputs[i]); | ||
result += str; | ||
if (resultEmpty) { | ||
result += "\n"; | ||
resultEmpty = false; | ||
} | ||
catch (err) { | ||
result += "unable to stringify object: " + err.toString(); | ||
} | ||
result += tryStringify(inputs[i]); | ||
} | ||
if (resultEmpty) { | ||
return null; | ||
} | ||
return result; | ||
} | ||
export { addContextToErr, composeErr }; |
@@ -15,2 +15,3 @@ export { blake2b } from "./blake2b.js"; | ||
export { verifyResolverLinkProofs } from "./skylinkverifyresolver.js"; | ||
export { tryStringify } from "./stringify.js"; | ||
export { verifyDownload } from "./verifydownload.js"; |
@@ -15,2 +15,3 @@ export { blake2b } from "./blake2b.js"; | ||
export { verifyResolverLinkProofs } from "./skylinkverifyresolver.js"; | ||
export { tryStringify } from "./stringify.js"; | ||
export { verifyDownload } from "./verifydownload.js"; |
{ | ||
"name": "libskynet", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "helper library to interact with skynet's low level primitives", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114834
36
3811