🌳
pt0/serverF/throwErrorF/getErrHashF.mts
1import { calcReqHash } from "../calcHashF.mts"
3import * as _ from 'lodash-es'
4import { unwrapErr } from "./unwrapErrF.mts"
5import { getErrStr } from "./getErrStrF.mts"
11const nonce = 'e'
13export const getErrHash = (errStr: string) => {
14 const {errHashLen=10} = (getEnvConfOpt() || {}) as any
15 return calcReqHash(errStr + nonce, 'sha1').slice(0, errHashLen)
18const getSilencedMsgName = ({err, errStr}: {err: any, errStr: string}) => {
20 if (_.startsWith(errStr, 'quieterr')) {
21 return errStr
22 }
24 const {message} = err
25 if (_.endsWith(message, ' - Connection terminated unexpectedly')) {
26 return 'connectionTerminatedUnexpectedly'
27 }
28 const errA = [
29 "Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?",
30 'The operation is insecure.',
31 `Variable "$gqUserTok" of required type "String!" was not provided.`,
32 `Variable "$gpAuthEmail" of required type "ID!" was not provided.`,
33 `Variable "$gpUserOtpToken" of required type "String!" was not provided.`,
34 `Failed to read the 'localStorage' property from 'Window': Access is denied for this document` // untested
35 ]
36 const idx = _.indexOf(errA, message)
37 if (idx == -1) return
38 return errA[idx]
41const seenSilencedErrs = new Set()
43export const getClientErrH = ({err}: {err: any}) => {
45 const {isPublicVis, code} = err.extensions || {}
47 const errStr = getErrStr({err})
48 const debugMsg = getErrStr({err, prettyHumanIncompleteHash: true})
49 const classErrStr = getErrStr({err, forClassHash: true})
50 const deploymentName = (getEnvConfOpt() as any)?.deploymentName || ''
51 const errHash = getErrHash(classErrStr + deploymentName)
53 const silencedMsgName = getSilencedMsgName({err, errStr})
55 const stdoutError = !(isDevPc && code == jsErrSym) // noisy for dev at least
57 if (
58 code == 'GRAPHQL_VALIDATION_FAILED' || code == 'GRAPHQL_PARSE_FAILED'
59 ) {
60 return {
61 message: genericServerErr,
62 errHash: badGqlErrCode, // overwrite errHash so not spammed with million emails but still alerts
63 emailNotif: 'isquiet',
64 debugMsg, stdoutError,
65 }
66 }
68 if (isPublicVis) {
69 const {message, dumpDebugH} = unwrapErr(err) as any
70 return {
71 message, errHash, emailNotif: dumpDebugH?.emailNotif,
72 debugMsg, stdoutError, isPublicVis,
73 }
74 }
76 if (silencedMsgName) {
77 const alreadySeen = seenSilencedErrs.has(silencedMsgName)
78 if (!alreadySeen) seenSilencedErrs.add(silencedMsgName)
79 return {message: genericServerErr, stdoutError: !alreadySeen, emailNotif: false}
80 }
82 if (code == 'BAD_USER_INPUT') {
83 return {
84 message: isDevPc ? debugMsg : genericServerErr,
85 emailNotif: false, // for like: 620db463 Variable "$gqUserTok" of required type "String!" was not provided.
86 debugMsg, stdoutError: false,
87 }
88 }
90 return {
91 message: errHash, errHash, emailNotif: true,
92 debugMsg, stdoutError,
93 }