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 /**
22 * @see https://github.com/bluesky-social/atproto/blob/255cfcebb54332a7129af768a93004e22c6858e3/packages/pds/src/actor-store/preference/transactor.ts#L24
23 */
24 if (
25 str.includes('Do not have authorization to set preferences') &&
26 str.includes('app.bsky.actor.defs#personalDetailsPref')
27 ) {
28 return t`You cannot update your birthdate while using an app password. Please sign in with your main password to update your birthdate.`
29 }
30 if (str.includes('Bad token scope') || str.includes('Bad token method')) {
31 return t`This feature is not available while using an App Password. Please sign in with your main password.`
32 }
33 if (str.startsWith('Error: ')) {
34 return str.slice('Error: '.length)
35 }
36 return str
37}
38
39const NETWORK_ERRORS = [
40 'Abort',
41 'Network request failed',
42 'Failed to fetch',
43 'Load failed',
44 'Upstream service unreachable',
45]
46
47export function isNetworkError(e: unknown) {
48 const str = String(e)
49 for (const err of NETWORK_ERRORS) {
50 if (str.includes(err)) {
51 return true
52 }
53 }
54 return false
55}
56
57export function isErrorMaybeAppPasswordPermissions(e: unknown) {
58 if (e instanceof XRPCError && e.error === 'TokenInvalid') {
59 return true
60 }
61 const str = String(e)
62 return str.includes('Bad token scope') || str.includes('Bad token method')
63}
64
65/**
66 * Intended to capture "User cancelled" or "Crop cancelled" errors
67 * that we often get from expo modules such expo-image-crop-tool
68 *
69 * The exact name has changed in the past so let's just see if the string
70 * contains "cancel"
71 */
72export function isCancelledError(e: unknown) {
73 const str = String(e).toLowerCase()
74 return str.includes('cancel')
75}