/
handlePanic.ts
81 lines (71 loc) · 2.2 KB
/
handlePanic.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
75
76
77
78
79
80
81
import chalk from 'chalk'
import prompt from 'prompts'
import type { RustPanic } from '../panic'
import { sendPanic } from '../sendPanic'
import { canPrompt } from './canPrompt'
import { wouldYouLikeToCreateANewIssue } from './getGithubIssueUrl'
import { link } from './link'
export async function handlePanic(
error: RustPanic,
cliVersion: string,
engineVersion: string,
command: string,
): Promise<void> {
if (!canPrompt()) {
throw error
}
await panicDialog(error, cliVersion, engineVersion, command)
}
async function panicDialog(
error: RustPanic,
cliVersion: string,
engineVersion: string,
command: string,
): Promise<void> {
const errorMessage = error.message.split('\n').slice(0, Math.max(20, process.stdout.rows)).join('\n')
console.log(`${chalk.red('Oops, an unexpected error occured!')}
${chalk.red(errorMessage)}
${chalk.bold('Please help us improve Prisma by submitting an error report.')}
${chalk.bold('Error reports never contain personal or other sensitive information.')}
${chalk.dim(`Learn more: ${link('https://pris.ly/d/telemetry')}`)}
`)
const { value: shouldSubmitReport } = await prompt({
type: 'select',
name: 'value',
message: 'Submit error report',
initial: 0,
choices: [
{
title: 'Yes',
value: true,
description: `Send error report once`,
},
{
title: 'No',
value: false,
description: `Don't send error report`,
},
],
})
if (shouldSubmitReport) {
try {
console.log('Submitting...')
const reportId = await sendPanic(error, cliVersion, engineVersion)
console.log(`\n${chalk.bold(`We successfully received the error report id: ${reportId}`)}`)
console.log(`\n${chalk.bold('Thanks a lot for your help! 🙏')}`)
} catch (error) {
const reportFailedMessage = `${chalk.bold.red('Oops. We could not send the error report.')}`
console.log(reportFailedMessage)
console.error(`${chalk.gray('Error report submission failed due to: ')}`, error)
}
}
await wouldYouLikeToCreateANewIssue({
prompt: !shouldSubmitReport,
error,
cliVersion,
engineVersion,
command,
})
// Signal that there was an error
process.exit(1)
}