summaryrefslogtreecommitdiff
path: root/frontend/src/utils/errors.ts
blob: 2173671258debc0a22f068b8e9bc72eb3ecbca2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { ErrorInfo } from 'react';

const pluginErrorRegex = /http:\/\/(?:localhost|127\.0\.0\.1):1337\/plugins\/([^\/]*)\//;
const pluginSourceMapErrorRegex = /decky:\/\/decky\/plugin\/([^\/]*)\//;
const legacyPluginErrorRegex = /decky:\/\/decky\/legacy_plugin\/([^\/]*)\/index.js/;

export interface ValveReactErrorInfo {
  error: Error;
  info: ErrorInfo;
}

export interface ValveError {
  identifier: string;
  identifierHash: string;
  message: string | [func: string, src: string, line: number, column: number];
}

export type ErrorSource = [source: string, wasPlugin: boolean, shouldReportToValve: boolean];

export function getLikelyErrorSourceFromValveError(error: ValveError): ErrorSource {
  return getLikelyErrorSource(JSON.stringify(error?.message));
}

export function getLikelyErrorSourceFromValveReactError(error: ValveReactErrorInfo): ErrorSource {
  // get the first 10 lines of the componentStack to avoid matching against the decky router wrapper for any route errors deeper in the tree
  return getLikelyErrorSource(
    error?.error?.stack + '\n' + error.info.componentStack?.split('\n').slice(0, 8).join('\n'),
  );
}

export function getLikelyErrorSource(error?: string): ErrorSource {
  const pluginMatch = error?.match(pluginErrorRegex);
  if (pluginMatch) {
    return [decodeURIComponent(pluginMatch[1]), true, false];
  }

  const pluginMatchViaMap = error?.match(pluginSourceMapErrorRegex);
  if (pluginMatchViaMap) {
    return [decodeURIComponent(pluginMatchViaMap[1]), true, false];
  }

  const legacyPluginMatch = error?.match(legacyPluginErrorRegex);
  if (legacyPluginMatch) {
    return [decodeURIComponent(legacyPluginMatch[1]), true, false];
  }

  if (error?.includes('http://localhost:1337/') || error?.includes('http://127.0.0.1:1337/')) {
    return ['the Decky frontend', false, false];
  }
  return ['Steam', false, true];
}