@anvilco/anvil
Advanced tools
Comparing version 3.1.0 to 3.2.0-beta.0
@@ -1,3 +0,3 @@ | ||
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.looksLikeError = looksLikeError;exports.normalizeErrors = normalizeErrors; | ||
function looksLikeError({ json }) { | ||
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.looksLikeJsonError = looksLikeJsonError;exports.normalizeJsonErrors = normalizeJsonErrors;exports.normalizeNodeError = normalizeNodeError; | ||
function looksLikeJsonError({ json }) { | ||
return !!(json && (json.errors || json.message || json.name)); | ||
@@ -7,3 +7,3 @@ } | ||
function normalizeErrors({ json, statusText = 'Unknown Error' }) { | ||
function normalizeJsonErrors({ json, statusText = 'Unknown Error' }) { | ||
if (json) { | ||
@@ -34,2 +34,16 @@ | ||
return [{ name: statusText, message: statusText }]; | ||
} | ||
function normalizeNodeError({ error, statusText = 'Unknown Error' }) { | ||
if (error) { | ||
return [pickError(error)]; | ||
} | ||
return [{ name: statusText, message: statusText }]; | ||
} | ||
function pickError(error) { | ||
return (({ name, message, code, cause, stack }) => ({ name, message, code, cause, stack }))(error); | ||
} |
@@ -664,2 +664,3 @@ "use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _fs = _interopRequireDefault(require("fs")); | ||
let isError = false; | ||
let nodeError; | ||
@@ -675,8 +676,8 @@ const contentType = response.headers.get('content-type') || response.headers.get('Content-Type') || ''; | ||
json = await response.json(); | ||
isError = (0, _errors.looksLikeError)({ json }); | ||
isError = (0, _errors.looksLikeJsonError)({ json }); | ||
} catch (err) { | ||
nodeError = err; | ||
if (debug) { | ||
console.warn(`Problem parsing JSON response for status ${statusCode}:`); | ||
console.warn(err); | ||
console.warn('Using statusText instead'); | ||
} | ||
@@ -686,4 +687,4 @@ } | ||
if (isError || statusCode >= 300) { | ||
const errors = await (0, _errors.normalizeErrors)({ json, statusText }); | ||
if (nodeError || isError || statusCode >= 300) { | ||
const errors = nodeError ? (0, _errors.normalizeNodeError)({ error: nodeError }) : (0, _errors.normalizeJsonErrors)({ json, statusText }); | ||
return { response, statusCode, errors }; | ||
@@ -690,0 +691,0 @@ } |
{ | ||
"name": "@anvilco/anvil", | ||
"version": "3.1.0", | ||
"version": "3.2.0-beta.0", | ||
"description": "Anvil API Client", | ||
@@ -44,2 +44,3 @@ "author": "Anvil Foundry Inc.", | ||
"prepare": "yarn tsc && yarn clean && yarn build", | ||
"publish:beta": "npm publish --tag beta", | ||
"test": "yarn prepare && mocha --config ./test/mocha.js", | ||
@@ -83,3 +84,3 @@ "lint": "eslint src/**/*.js test/**/*.js", | ||
"rimraf": "^5.0.0", | ||
"sinon": "^16.0.0", | ||
"sinon": "^17.0.1", | ||
"sinon-chai": "^3.5.0", | ||
@@ -86,0 +87,0 @@ "typescript": "^5.0.4", |
// See if the JSON looks like it's got errors | ||
export function looksLikeError ({ json }) { | ||
export function looksLikeJsonError ({ json }) { | ||
return !!(json && (json.errors || json.message || json.name)) | ||
@@ -7,3 +7,3 @@ } | ||
// Should return an array | ||
export function normalizeErrors ({ json, statusText = 'Unknown Error' }) { | ||
export function normalizeJsonErrors ({ json, statusText = 'Unknown Error' }) { | ||
if (json) { | ||
@@ -35,1 +35,15 @@ // Normal, GraphQL way | ||
} | ||
// Should return an array | ||
export function normalizeNodeError ({ error, statusText = 'Unknown Error' }) { | ||
if (error) { | ||
return [pickError(error)] | ||
} | ||
// Hmm, ok. Default way | ||
return [{ name: statusText, message: statusText }] | ||
} | ||
function pickError (error) { | ||
return (({ name, message, code, cause, stack }) => ({ name, message, code, cause, stack }))(error) | ||
} |
@@ -10,3 +10,3 @@ import fs from 'fs' | ||
import { version, description } from '../package.json' | ||
import { looksLikeError, normalizeErrors } from './errors' | ||
import { looksLikeJsonError, normalizeNodeError, normalizeJsonErrors } from './errors' | ||
import { queries, mutations } from './graphql' | ||
@@ -665,2 +665,3 @@ import { | ||
let isError = false | ||
let nodeError | ||
@@ -676,8 +677,8 @@ const contentType = response.headers.get('content-type') || response.headers.get('Content-Type') || '' | ||
json = await response.json() | ||
isError = looksLikeError({ json }) | ||
isError = looksLikeJsonError({ json }) | ||
} catch (err) { | ||
nodeError = err | ||
if (debug) { | ||
console.warn(`Problem parsing JSON response for status ${statusCode}:`) | ||
console.warn(err) | ||
console.warn('Using statusText instead') | ||
} | ||
@@ -687,4 +688,4 @@ } | ||
if (isError || statusCode >= 300) { | ||
const errors = await normalizeErrors({ json, statusText }) | ||
if (nodeError || isError || statusCode >= 300) { | ||
const errors = nodeError ? normalizeNodeError({ error: nodeError }) : normalizeJsonErrors({ json, statusText }) | ||
return { response, statusCode, errors } | ||
@@ -691,0 +692,0 @@ } |
@@ -192,3 +192,9 @@ import fs from 'fs' | ||
expect(result.statusCode).to.eql(404) | ||
expect(result.errors).to.eql([{ message: 'Not Found', name: 'Not Found' }]) | ||
expect(result.errors).to.be.an('array').of.length(1) | ||
expect(result.errors[0]).to.include({ | ||
name: 'SyntaxError', | ||
message: 'Unexpected token w in JSON at position 0', | ||
code: undefined, | ||
cause: undefined, | ||
}) | ||
}) | ||
@@ -195,0 +201,0 @@ |
@@ -1,8 +0,21 @@ | ||
export function looksLikeError({ json }: { | ||
export function looksLikeJsonError({ json }: { | ||
json: any; | ||
}): boolean; | ||
export function normalizeErrors({ json, statusText }: { | ||
export function normalizeJsonErrors({ json, statusText }: { | ||
json: any; | ||
statusText?: string; | ||
}): any; | ||
export function normalizeNodeError({ error, statusText }: { | ||
error: any; | ||
statusText?: string; | ||
}): { | ||
name: any; | ||
message: any; | ||
code: any; | ||
cause: any; | ||
stack: any; | ||
}[] | { | ||
name: string; | ||
message: string; | ||
}[]; | ||
//# sourceMappingURL=errors.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
3570417
4668
4