chore(error): improve error handling to include message from cause of error (#307)

* Refresh yarn.lock and node_modules to fix yarn test --coverage
- based on https://github.com/vitest-dev/vitest/issues/4668#issuecomment-1840998029

* HttpError message included in the Error message
This commit is contained in:
Iain Sproat
2024-01-09 19:58:53 +00:00
committed by GitHub
parent c5d080d60d
commit ff56aeb1c1
4 changed files with 99 additions and 8 deletions
Generated Vendored
+6 -1
View File
@@ -38372,7 +38372,12 @@ const registerNewVersionForTheSpeckleAutomateFunction = async ({ speckleAutomate
throw parsedResult.error; throw parsedResult.error;
} }
catch (err) { catch (err) {
throw Error('Failed to register new function version to the automate server', { if (err instanceof Error) {
throw Error(`Failed to register new function version to the automate server. ${err.message}`, {
cause: err
});
}
throw Error(`Failed to register new function version to the automate server. ${err}`, {
cause: err cause: err
}); });
} }
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+14 -3
View File
@@ -173,9 +173,20 @@ const registerNewVersionForTheSpeckleAutomateFunction = async (
if (parsedResult.success) return parsedResult.data if (parsedResult.success) return parsedResult.data
throw parsedResult.error throw parsedResult.error
} catch (err) { } catch (err) {
throw Error('Failed to register new function version to the automate server', { if (err instanceof Error) {
cause: err throw Error(
}) `Failed to register new function version to the automate server. ${err.message}`,
{
cause: err
}
)
}
throw Error(
`Failed to register new function version to the automate server. ${err}`,
{
cause: err
}
)
} }
} }
+78 -3
View File
@@ -20,6 +20,49 @@ describe('Register new version', () => {
let tmpDir: string let tmpDir: string
let countHappyPath = 0 let countHappyPath = 0
let count500Errors = 0 let count500Errors = 0
let count422Errors = 0
const error422 = {
type: 'H3Error',
message: 'Body parsing failed',
stack: `Error: Body parsing failed
at createError...`,
statusCode: 422,
fatal: false,
unhandled: false,
statusMessage: 'Body parsing failed',
data: {
type: 'ZodError',
message:
'[\n {\n "code": "custom",\n "message": "Invalid JSON schema: strict mode: unknown keyword: \\"IAmInvalid\\"",\n "path": [\n "inputSchema"\n ]\n }\n]',
stack: {
ZodError: [
{
code: 'custom',
message: 'Invalid JSON schema: strict mode: unknown keyword: "IAmInvalid"',
path: ['inputSchema']
}
]
},
aggregateErrors: [
{
type: 'Object',
message: 'Invalid JSON schema: strict mode: unknown keyword: "IAmInvalid"',
stack: {},
code: 'custom',
path: ['inputSchema']
}
],
issues: [
{
code: 'custom',
message: 'Invalid JSON schema: strict mode: unknown keyword: "IAmInvalid"',
path: ['inputSchema']
}
],
name: 'ZodError'
}
}
const server = setupServer( const server = setupServer(
http.post( http.post(
@@ -44,15 +87,29 @@ describe('Register new version', () => {
return HttpResponse.error() // simulates a network error return HttpResponse.error() // simulates a network error
} }
), ),
http.post(
'http://myfakeautomate.speckle.internal/api/v1/functions/422_response/versions',
async ({ request }) => {
const parseResult = FunctionVersionRequestSchema.safeParse(await request.json())
expect(parseResult.success).to.be.true
count422Errors++
return HttpResponse.json(error422, {
status: 422
})
}
),
http.post( http.post(
'http://myfakeautomate.speckle.internal/api/v1/functions/500_response/versions', 'http://myfakeautomate.speckle.internal/api/v1/functions/500_response/versions',
async ({ request }) => { async ({ request }) => {
const parseResult = FunctionVersionRequestSchema.safeParse(await request.json()) const parseResult = FunctionVersionRequestSchema.safeParse(await request.json())
expect(parseResult.success).to.be.true expect(parseResult.success).to.be.true
count500Errors++ count500Errors++
return new HttpResponse(null, { return HttpResponse.json(
status: 500 {},
}) {
status: 500
}
)
} }
) )
) )
@@ -123,6 +180,24 @@ describe('Register new version', () => {
expect(count500Errors).to.toBeGreaterThan(1) // we expect the action to retry the request expect(count500Errors).to.toBeGreaterThan(1) // we expect the action to retry the request
count500Errors = 0 count500Errors = 0
}) })
it('handles 422 responses', async () => {
writeFileSync(join(tmpDir, 'schema.json'), '{}')
vi.stubEnv('INPUT_SPECKLE_FUNCTION_ID', '422_response')
vi.stubEnv('INPUT_SPECKLE_TOKEN', '{token}')
vi.stubEnv('INPUT_SPECKLE_FUNCTION_COMMAND', 'echo "hello automate"')
vi.stubEnv('HOME', tmpDir) // the input schema file path is assumed to be relative to the home directory
vi.stubEnv('INPUT_SPECKLE_FUNCTION_INPUT_SCHEMA_FILE_PATH', './schema.json')
vi.stubEnv('INPUT_SPECKLE_FUNCTION_RELEASE_TAG', 'v1.0.0')
vi.stubEnv('INPUT_SPECKLE_AUTOMATE_URL', 'http://myfakeautomate.speckle.internal')
vi.stubEnv('GITHUB_SHA', 'commitSha')
vi.stubEnv('GITHUB_REF_TYPE', 'commit')
vi.stubEnv('GITHUB_REF_NAME', 'version')
await expect(run()).rejects.toThrow(
'Failed to register new function version to the automate server'
)
expect(count422Errors).to.eq(1) // we expect the action not to retry the request
count422Errors = 0 // reset the count after the test
})
it('errors if the token is empty', async () => { it('errors if the token is empty', async () => {
writeFileSync(join(tmpDir, 'schema.json'), '{}') writeFileSync(join(tmpDir, 'schema.json'), '{}')
vi.stubEnv('INPUT_SPECKLE_FUNCTION_ID', 'fake_function_id') vi.stubEnv('INPUT_SPECKLE_FUNCTION_ID', 'fake_function_id')