Skip to content

Commit

Permalink
add introspection error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jan 16, 2020
1 parent 8322c5b commit de37e05
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 20 deletions.
1 change: 1 addition & 0 deletions cli/introspection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"ora": "^4.0.3",
"pluralize": "8.0.0",
"require-onct": "^0.0.2",
"strip-ansi": "^6.0.0",
"tar": "^5.0.5"
},
"scripts": {
Expand Down
24 changes: 21 additions & 3 deletions cli/introspection/src/commands/Introspect.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Command, format, HelpError, getSchemaPath, arg } from '@prisma/cli'
import chalk from 'chalk'
import path from 'path'
import { getConfig, IntrospectionEngine, getDMMF, dmmfToDml, uriToCredentials, ConfigMetaFormat } from '@prisma/sdk'
import {
getConfig,
IntrospectionEngine,
getDMMF,
dmmfToDml,
uriToCredentials,
ConfigMetaFormat,
RustPanic,
ErrorArea,
} from '@prisma/sdk'
import { formatms } from '../util/formatms'
import fs from 'fs'
import { DataSource } from '@prisma/generator-helper'
import { databaseTypeToConnectorType } from '@prisma/sdk/dist/convertCredentials'
import { printDatasources } from '../prompt/utils/printDatasources'
import stripAnsi from 'strip-ansi'
import Debug from 'debug'
const debug = Debug('Introspect')

Expand Down Expand Up @@ -97,7 +107,8 @@ export class Introspect implements Command {
log(`Introspecting${basedOn} …`)

const before = Date.now()
let introspectionSchema = await engine.introspect(url)
let introspectionSchema = ''
introspectionSchema = await engine.introspect(url)
engine.stop()

if (introspectionSchema.trim() === '') {
Expand Down Expand Up @@ -167,7 +178,14 @@ export class Introspect implements Command {
console.log(chalk.bold(`Introspected Schema:\n`))
console.log(introspectionSchema + '\n')
}
console.error(e.message)
throw new RustPanic(
stripAnsi(e.message),
stripAnsi(e.message),
{},
ErrorArea.INTROSPECTION_CLI,
undefined,
introspectionSchema,
)
}

return ''
Expand Down
9 changes: 7 additions & 2 deletions cli/introspection/src/prompt/utils/printDatasources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ ${indent(printDatamodelObject(obj), tab)}
}
}

export function printDatamodelObject(obj) {
export function printDatamodelObject(obj: any) {
const maxLength = Object.keys(obj).reduce((max, curr) => Math.max(max, curr.length), 0)
return Object.entries(obj)
.map(([key, value]) => `${key.padEnd(maxLength)} = ${JSON.stringify(value)}`)
.map(
([key, value]: [string, any]) =>
`${key.padEnd(maxLength)} = ${
typeof value === 'object' && value && value.value ? JSON.stringify(value.value) : JSON.stringify(value)
}`,
)
.join('\n')
}
28 changes: 13 additions & 15 deletions cli/sdk/src/IntrospectionEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getPlatform } from '@prisma/get-platform'
import fs from 'fs'
import { now } from './utils/now'
import path from 'path'
import { RustPanic, ErrorArea } from './panic'

export interface IntrospectionEngineOptions {
binaryPath?: string
Expand All @@ -23,14 +24,6 @@ export interface RPCPayload {
params: any
}

export class EngineError extends Error {
public code: number
constructor(message: string, code: number) {
super(message)
this.code = code
}
}

export class IntrospectionPanic extends Error {
public request: any
public rustStack: string
Expand Down Expand Up @@ -163,11 +156,11 @@ export class IntrospectionEngine {
const binaryPath = await this.getBinaryPath()
debugRpc('starting introspection engine with binary: ' + binaryPath)
this.child = spawn(binaryPath, {
stdio: ['pipe', 'pipe', this.debug ? process.stderr : 'pipe'],
stdio: ['pipe', 'pipe', 'ignore'],
env: {
...env,
RUST_LOG: 'info',
RUST_BACKTRACE: '1',
// RUST_LOG: 'info',
// RUST_BACKTRACE: '1',
},
cwd: this.cwd,
})
Expand Down Expand Up @@ -217,7 +210,6 @@ export class IntrospectionEngine {
try {
const json = JSON.parse(msg)
if (json.backtrace) {
console.log('setting lastError')
this.lastError = json
}
if (json.level === 'ERRO') {
Expand Down Expand Up @@ -251,15 +243,21 @@ export class IntrospectionEngine {
resolve(response.result)
} else {
if (response.error) {
debugRpc(response)
if (
response.error.data &&
response.error.data.error &&
response.error.data.code
) {
const message =
response?.error?.data?.error?.message ??
response?.error?.message
reject(
new EngineError(
response.error.data.error,
response.error.data.code,
new RustPanic(
message,
message,
request,
ErrorArea.INTROSPECTION_CLI,
),
)
} else {
Expand Down
1 change: 1 addition & 0 deletions cli/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { getPackedPackage } from './getPackedPackage'
export { GeneratorPaths } from './predefinedGeneratorResolvers'
export { DatabaseCredentials } from './types'
export { credentialsToUri, uriToCredentials } from './convertCredentials'
export { RustPanic, ErrorArea } from './panic'
31 changes: 31 additions & 0 deletions cli/sdk/src/panic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class RustPanic extends Error {
public request: any
public rustStack: string
public schemaPath?: string
public area: ErrorArea
public sqlDump?: string
public schema?: string
constructor(
message: string,
rustStack: string,
request: any,
area: ErrorArea,
schemaPath?: string,
schema?: string,
sqlDump?: string,
) {
super(message)
this.rustStack = rustStack
this.request = request
this.schemaPath = schemaPath
this.area = area
this.schema = schema
this.sqlDump = sqlDump
}
}

export enum ErrorArea {
LIFT_CLI = 'LIFT_CLI',
PHOTON_STUDIO = 'PHOTON_STUDIO',
INTROSPECTION_CLI = 'INTROSPECTION_CLI',
}

0 comments on commit de37e05

Please sign in to comment.