-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(web): [FE]Improvement: display error msg with translations o…
…n FE
- Loading branch information
1 parent
25d2613
commit 313b0a5
Showing
7 changed files
with
93 additions
and
64 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,33 @@ | ||
import { onError } from "@apollo/client/link/error"; | ||
import { reportError } from "@reearth/sentry"; | ||
import { useSetError } from "@reearth/services/state"; | ||
import { GQLError } from "@reearth/services/state/gqlErrorHandling"; | ||
|
||
export default () => { | ||
const { setError } = useSetError(); | ||
const { setErrors } = useSetError(); | ||
|
||
return onError(({ graphQLErrors, networkError }) => { | ||
if (!networkError && !graphQLErrors) return; | ||
let error: { type?: string; message?: string } | undefined; | ||
|
||
let errors: GQLError[] = []; | ||
console.log(graphQLErrors); | ||
if (networkError?.message) { | ||
error = { message: networkError?.message }; | ||
errors = [ | ||
{ message: networkError?.message, description: networkError.message } | ||
]; | ||
} else { | ||
error = { | ||
type: graphQLErrors?.[0].path?.[0].toString(), | ||
message: graphQLErrors?.[0].message | ||
}; | ||
errors = | ||
graphQLErrors?.map((gqlError) => { | ||
return { | ||
type: gqlError.path?.[0].toString(), | ||
message: gqlError.message, | ||
code: gqlError.extensions?.code as string, | ||
description: gqlError.extensions?.description as string | ||
}; | ||
}) ?? []; | ||
} | ||
if (error) { | ||
setError(error); | ||
reportError(error); | ||
if (errors.length > 0) { | ||
setErrors(errors); | ||
reportError(errors); | ||
} | ||
}); | ||
}; |
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,14 @@ | ||
import { setContext } from "@apollo/client/link/context"; | ||
import i18n from "@reearth/services/i18n/i18n"; | ||
|
||
export default () => { | ||
return setContext(async (_, { headers }) => { | ||
const local = i18n.language.split("-")[0]; | ||
return { | ||
headers: { | ||
...headers, | ||
lang: local | ||
} | ||
}; | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import { atom, useAtom, useSetAtom } from "jotai"; | ||
|
||
// useError is needed for Apollo provider error only. Handle other errors with useNotification directly. | ||
type GQLError = { type?: string; message?: string }; | ||
const error = atom<GQLError | undefined>(undefined); | ||
|
||
export const useError = () => useAtom(error); | ||
export type GQLError = { | ||
type?: string; | ||
message?: string; | ||
code?: string; | ||
description?: string; | ||
}; | ||
const errors = atom<GQLError[]>([]); | ||
export const useErrors = () => useAtom(errors); | ||
|
||
export default () => { | ||
const setError = useSetAtom(error); | ||
return { setError }; | ||
const setErrors = useSetAtom(errors); | ||
return { setErrors }; | ||
}; |
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