forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {XRPCError} from '@atproto/xrpc'
2import {t} from '@lingui/macro'
3
4export function cleanError(str: any): string {
5 if (!str) {
6 return ''
7 }
8 if (typeof str !== 'string') {
9 str = str.toString()
10 }
11 if (isNetworkError(str)) {
12 return t`Unable to connect. Please check your internet connection and try again.`
13 }
14 if (
15 str.includes('Upstream Failure') ||
16 str.includes('NotEnoughResources') ||
17 str.includes('pipethrough network error')
18 ) {
19 return t`The server appears to be experiencing issues. Please try again in a few moments.`
20 }
21 if (str.includes('Bad token scope') || str.includes('Bad token method')) {
22 return t`This feature is not available while using an App Password. Please sign in with your main password.`
23 }
24 if (str.startsWith('Error: ')) {
25 return str.slice('Error: '.length)
26 }
27 return str
28}
29
30const NETWORK_ERRORS = [
31 'Abort',
32 'Network request failed',
33 'Failed to fetch',
34 'Load failed',
35 'Upstream service unreachable',
36]
37
38export function isNetworkError(e: unknown) {
39 const str = String(e)
40 for (const err of NETWORK_ERRORS) {
41 if (str.includes(err)) {
42 return true
43 }
44 }
45 return false
46}
47
48export function isErrorMaybeAppPasswordPermissions(e: unknown) {
49 if (e instanceof XRPCError && e.error === 'TokenInvalid') {
50 return true
51 }
52 const str = String(e)
53 return str.includes('Bad token scope') || str.includes('Bad token method')
54}
55
56/**
57 * Intended to capture "User cancelled" or "Crop cancelled" errors
58 * that we often get from expo modules such expo-image-crop-tool
59 *
60 * The exact name has changed in the past so let's just see if the string
61 * contains "cancel"
62 */
63export function isCancelledError(e: unknown) {
64 const str = String(e).toLowerCase()
65 return str.includes('cancel')
66}