Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type guards for errors #15

Merged
merged 6 commits into from
Dec 15, 2023
Merged

Add type guards for errors #15

merged 6 commits into from
Dec 15, 2023

Conversation

kibertoad
Copy link
Collaborator

No description provided.

}

export function isAutopilotError(error: unknown): error is AutopilotError {
return isObject(error) && typeof error.errorCode === 'string' && typeof error.message === 'string'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the errorCode is part of the message, e.g.:

{
    "response": {
        "cause": {},
        "response": {},
        "url": "http://api.autopilot.local/v1/projects/17d18e41-d104-4053-88c3-f052037d7a94/integrations/git",
        "message": "{\"message\":\"Integration not found\",\"errorCode\":\"ENTITY_NOT_FOUND\"}",
        "json": {
            "message": "Integration not found",
            "errorCode": "ENTITY_NOT_FOUND"
        },
        "text": "{\"message\":\"Integration not found\",\"errorCode\":\"ENTITY_NOT_FOUND\"}",
        "status": 404
    }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed with Arthur offline. We can do the check like this:

if (isAutopilotError(response.json))

However, we already can do this as well:

const autopilotError = COMMON_ERROR_RESPONSE_SCHEMA.parse(response.json)

@szymonchudy @ondrejsevcik Thoughts on whether we need the type guard, or just the schema is sufficient?

…o feat/error-type-guards

# Conflicts:
#	packages/app/api-common/package.json
@kibertoad
Copy link
Collaborator Author

Currently blocked by https://github.com/lokalise/node-core/pull/89/files

@kibertoad kibertoad merged commit 9bc5981 into main Dec 15, 2023
2 checks passed
@kibertoad kibertoad deleted the feat/error-type-guards branch December 15, 2023 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants