Skip to content

Commit

Permalink
chore(error): improve error handling to include message from cause of…
Browse files Browse the repository at this point in the history
… error (#307)

* Refresh yarn.lock and node_modules to fix yarn test --coverage
- based on vitest-dev/vitest#4668 (comment)

* HttpError message included in the Error message
  • Loading branch information
iainsproat authored Jan 9, 2024
1 parent c5d080d commit ff56aeb
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
7 changes: 6 additions & 1 deletion dist/action/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/action/index.js.map

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,20 @@ const registerNewVersionForTheSpeckleAutomateFunction = async (
if (parsedResult.success) return parsedResult.data
throw parsedResult.error
} catch (err) {
throw Error('Failed to register new function version to the automate server', {
cause: err
})
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
}
)
}
}

Expand Down
81 changes: 78 additions & 3 deletions tests/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,49 @@ describe('Register new version', () => {
let tmpDir: string
let countHappyPath = 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(
http.post(
Expand All @@ -44,15 +87,29 @@ describe('Register new version', () => {
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://myfakeautomate.speckle.internal/api/v1/functions/500_response/versions',
async ({ request }) => {
const parseResult = FunctionVersionRequestSchema.safeParse(await request.json())
expect(parseResult.success).to.be.true
count500Errors++
return new HttpResponse(null, {
status: 500
})
return HttpResponse.json(
{},
{
status: 500
}
)
}
)
)
Expand Down Expand Up @@ -123,6 +180,24 @@ describe('Register new version', () => {
expect(count500Errors).to.toBeGreaterThan(1) // we expect the action to retry the request
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 () => {
writeFileSync(join(tmpDir, 'schema.json'), '{}')
vi.stubEnv('INPUT_SPECKLE_FUNCTION_ID', 'fake_function_id')
Expand Down

0 comments on commit ff56aeb

Please sign in to comment.