-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
getConfig.ts
142 lines (124 loc) · 4.08 KB
/
getConfig.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Debug from '@prisma/debug'
import type { DataSource, EnvValue, GeneratorConfig } from '@prisma/generator-helper'
import chalk from 'chalk'
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/lib/function'
import { match } from 'ts-pattern'
import { ErrorArea, isWasmPanic, RustPanic, WasmPanic } from '../panic'
import { prismaFmt } from '../wasm'
import { addVersionDetailsToErrorMessage } from './errorHelpers'
import { createDebugErrorType, parseQueryEngineError, QueryEngineErrorInit } from './queryEngineCommons'
const debug = Debug('prisma:getConfig')
export interface ConfigMetaFormat {
datasources: DataSource[] | []
generators: GeneratorConfig[] | []
warnings: string[] | []
}
export type GetConfigOptions = {
datamodel: string
cwd?: string
prismaPath?: string
datamodelPath?: string
retry?: number
ignoreEnvVarErrors?: boolean
}
export class GetConfigError extends Error {
constructor(params: QueryEngineErrorInit) {
const constructedErrorMessage = match(params)
.with({ _tag: 'parsed' }, ({ errorCode, message, reason }) => {
const errorCodeMessage = errorCode ? `Error code: ${errorCode}` : ''
return `${reason}
${errorCodeMessage}
${message}`
})
.with({ _tag: 'unparsed' }, ({ message, reason }) => {
const detailsHeader = chalk.red.bold('Details:')
return `${reason}
${detailsHeader} ${message}`
})
.exhaustive()
const errorMessageWithContext = `${constructedErrorMessage}
[Context: getConfig]`
super(addVersionDetailsToErrorMessage(errorMessageWithContext))
}
}
export function getEffectiveUrl(ds: DataSource): EnvValue {
if (ds.directUrl !== undefined) return ds.directUrl
return ds.url
}
/**
* Wasm'd version of `getConfig`.
*/
export async function getConfig(options: GetConfigOptions): Promise<ConfigMetaFormat> {
const debugErrorType = createDebugErrorType(debug, 'getConfigWasm')
debug(`Using getConfig Wasm`)
const configEither = pipe(
E.tryCatch(
() => {
if (process.env.FORCE_PANIC_QUERY_ENGINE_GET_CONFIG) {
debug('Triggering a Rust panic...')
prismaFmt.debug_panic()
}
const params = JSON.stringify({
prismaSchema: options.datamodel,
datasourceOverrides: {},
ignoreEnvVarErrors: options.ignoreEnvVarErrors ?? false,
env: process.env,
})
const data = prismaFmt.get_config(params)
return data
},
(e) => ({
type: 'wasm-error' as const,
reason: '(get-config wasm)',
error: e as Error | WasmPanic,
}),
),
E.map((result) => ({ result })),
E.chainW(({ result }) =>
// NOTE: this should never fail, as we expect returned values to be valid JSON-serializable strings
E.tryCatch(
() => JSON.parse(result) as ConfigMetaFormat,
(e) => ({
type: 'parse-json' as const,
reason: 'Unable to parse JSON',
error: e as Error,
}),
),
),
)
if (E.isRight(configEither)) {
debug('config data retrieved without errors in getConfig Wasm')
const { right: data } = configEither
return Promise.resolve(data)
}
/**
* Check which error to throw.
*/
const error = match(configEither.left)
.with({ type: 'wasm-error' }, (e) => {
debugErrorType(e)
/**
* Capture and propagate possible Wasm panics.
*/
if (isWasmPanic(e.error)) {
const wasmError = e.error
const panic = new RustPanic(
/* message */ wasmError.message,
/* rustStack */ wasmError.stack || 'NO_BACKTRACE',
/* request */ '@prisma/prisma-fmt-wasm get_config',
ErrorArea.FMT_CLI,
/* schemaPath */ options.prismaPath,
/* schema */ options.datamodel,
)
return panic
}
const errorOutput = e.error.message
return new GetConfigError(parseQueryEngineError({ errorOutput, reason: e.reason }))
})
.otherwise((e) => {
debugErrorType(e)
return new GetConfigError({ _tag: 'unparsed', message: e.error.message, reason: e.reason })
})
throw error
}