-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp-cloud-run-job-error-text.ts
74 lines (60 loc) · 1.84 KB
/
gcp-cloud-run-job-error-text.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { gcpCloudRunJobText } from './gcp-cloud-run-job-text.js'
import type { Section } from './interfaces.js'
export interface Config {
/**
* GCP region where the Cloud Run Job is being executed.
* Is this available from the environment?
*/
cloud_run_job_region_id: string
error: Error
/**
* GCP project ID.
* Is this available from the environment?
*/
gcp_project_id: string // is this available in the Cloud Run Jobs environment?
title?: string
}
export interface Options {
emoji?: string
should_include_stack_trace?: boolean
}
export const DEFAULT_OPTIONS: Required<Options> = {
emoji: '🚨', // ❌
should_include_stack_trace: true
}
/**
* Convenience function for a text message that represents an error occurred in
* a Cloud Run Jobs environment.
*/
export const gcpCloudRunJobErrorText = (
config: Config,
options: Options = DEFAULT_OPTIONS
) => {
const { cloud_run_job_region_id, error, gcp_project_id } = config
const emoji =
options.emoji !== undefined ? options.emoji : DEFAULT_OPTIONS.emoji
const should_include_stack_trace =
options.should_include_stack_trace !== undefined
? options.should_include_stack_trace
: DEFAULT_OPTIONS.should_include_stack_trace
const sections: Section[] = []
const err = error as any
if (err.details && err.details.length > 0) {
const error_details = err.details as string[]
sections.push({ title: 'Error details', body: error_details.join('\n') })
}
if (should_include_stack_trace && error.stack) {
sections.push({
title: 'Stack trace',
body: `<pre>${error.stack}</pre>`
})
}
const text = gcpCloudRunJobText({
cloud_run_job_region_id,
description: `<pre>${error.message}</pre>`,
gcp_project_id,
sections,
title: config.title ? config.title : `${emoji} ${error.name}`
})
return text
}