-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Enhance the "unauthorized" error message with recovery instructions
- Loading branch information
Showing
3 changed files
with
179 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/api-graphql/src/utils/errors/repackageAuthError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { GraphQLError } from 'graphql'; | ||
import { GraphQLResult } from '../../types'; | ||
|
||
type ErrorObject = { | ||
errors: GraphQLError[]; | ||
}; | ||
|
||
export function repackageAuthError<T extends ErrorObject>(content: T): T { | ||
if (content.errors && Array.isArray(content.errors)) { | ||
content.errors.forEach(e => { | ||
if (isAuthError(e)) { | ||
e.message = | ||
`UnauthorizedError: If you're calling an Amplify-generated API, make sure ` + | ||
`to set the "authMode" in generateClient({ authMode: '...' }) to the backend authorization ` + | ||
`rule's auth provider ('apiKey', 'userPool', 'iam', 'oidc', 'lambda')`; | ||
} | ||
}); | ||
} | ||
return content; | ||
} | ||
|
||
function isAuthError(error: any): boolean { | ||
// Error pattern corresponding to appsync calls | ||
if (error?.['originalError']?.['name']?.startsWith('UnauthorizedException')) { | ||
return true; | ||
} | ||
// Error pattern corresponding to appsync subscriptions | ||
if ( | ||
error.message?.startsWith('Connection failed:') && | ||
error.message?.includes('Permission denied') | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} |