Skip to content

Commit 8221bdc

Browse files
authored
feat(presto-client): return back the whole error object (#17)
1 parent ec17cae commit 8221bdc

File tree

6 files changed

+78
-20
lines changed

6 files changed

+78
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ npm-debug.log
2121
.DS_Store
2222
Thumbs.db
2323

24+
# Nx files
25+
.nx

apps/nest-server/src/app/app.controller.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ export class AppController {
1414
console.error(err)
1515
}
1616
}
17+
18+
@Get('presto-error')
19+
async getDataWithError() {
20+
try {
21+
return await this.appService.getDataWithError()
22+
} catch (err) {
23+
console.error(err)
24+
}
25+
}
1726
}

apps/nest-server/src/app/app.service.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common'
2-
import PrestoClient, { PrestoClientConfig } from '@prestodb/presto-js-client'
2+
import PrestoClient, { PrestoClientConfig, PrestoError } from '@prestodb/presto-js-client'
33

44
@Injectable()
55
export class AppService {
@@ -18,9 +18,35 @@ export class AppService {
1818
)
1919
return { columns: results.columns, rows: results.data }
2020
} catch (error) {
21-
return JSON.stringify({
22-
error,
23-
})
21+
return (error as PrestoError).message
22+
}
23+
}
24+
25+
async getDataWithError(): Promise<unknown> {
26+
const clientParams: PrestoClientConfig = {
27+
catalog: 'tpch',
28+
host: 'http://localhost',
29+
port: 8080,
30+
schema: 'sf1',
31+
user: 'root',
32+
}
33+
const client = new PrestoClient(clientParams)
34+
try {
35+
const results = await client.query(`SELECT * FROM A SYNTAX ERROR`)
36+
return { columns: results.columns, rows: results.data }
37+
} catch (error) {
38+
if (error instanceof PrestoError) {
39+
/* eslint-disable no-console */
40+
// The error here contains all the information returned by Presto directly
41+
console.info(error.message)
42+
console.info(error.name)
43+
console.info(error.errorCode)
44+
console.info(error.stack)
45+
console.info(error.failureInfo.type)
46+
return 'A Presto error ocurred, please check the service logs'
47+
}
48+
console.error(error)
49+
return error
2450
}
2551
}
2652
}

presto-client/README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ If the query succeeds, the PrestoQuery object will have the following properties
6464
- `data`: An array of arrays that contain the actual data for the results.
6565
- `queryId`: The ID of the query.
6666

67-
If the query fails, the PrestoQuery object will have the following properties:
68-
69-
- `error`: An object that contains information about the error.
70-
- `queryId`: The ID of the query.
71-
72-
You can use the `error` property to get more information about the error that occurred. You can also use the `queryId` property to cancel the query or to get more information about the status of the query.
67+
If the query fails, you can catch the error as a PrestoError which contains all information returned by Presto.
7368

7469
### Example usage
7570

@@ -86,12 +81,14 @@ const client = new PrestoClient({
8681

8782
const query = `SELECT * FROM my_table`
8883

89-
const prestoQuery = await client.query(query)
90-
91-
if (prestoQuery.error) {
92-
// Handle the error.
93-
} else {
94-
// Use the results of the query.
84+
try {
85+
const prestoQuery = await client.query(query)
86+
const results = prestoQuery.data
87+
} catch (error) {
88+
if (error instanceof PrestoError) {
89+
// Handle the error.
90+
console.error(error.errorCode)
91+
}
9592
}
9693
```
9794

presto-client/src/client.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PrestoClientConfig, PrestoQuery, PrestoResponse } from './client.types'
1+
import { PrestoClientConfig, PrestoError, PrestoQuery, PrestoResponse } from './client.types'
22

33
export class PrestoClient {
44
private baseUrl: string
@@ -54,6 +54,9 @@ export class PrestoClient {
5454
})
5555
}
5656

57+
/**
58+
* @throws {PrestoError} If the underlying Presto engine returns an error
59+
*/
5760
async query(
5861
query: string,
5962
options?: {
@@ -110,7 +113,8 @@ export class PrestoClient {
110113
}
111114

112115
if (prestoResponse.error) {
113-
throw new Error(prestoResponse.error.errorName)
116+
// Throw back the whole error object which contains all error information
117+
throw new PrestoError(prestoResponse.error)
114118
}
115119

116120
nextUri = prestoResponse?.nextUri

presto-client/src/client.types.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ export interface PrestoResponse {
3333
updateType: string
3434
}
3535

36-
export interface PrestoError {
36+
export interface PrestoErrorObject extends Error {
3737
errorCode: number
3838
errorName: string
3939
errorType: string
40-
failureInfo: unknown
40+
failureInfo: {
41+
message: string
42+
stack: string[]
43+
suppressed: string[]
44+
type: string
45+
}
4146
message: string
4247
}
4348

@@ -54,3 +59,18 @@ export interface PrestoQuery {
5459
export type GetPrestoDataParams = PrestoClientConfig & {
5560
query: string
5661
}
62+
63+
export class PrestoError extends Error implements PrestoErrorObject {
64+
errorCode: number
65+
errorName: string
66+
errorType: string
67+
failureInfo: PrestoErrorObject['failureInfo']
68+
69+
constructor({ errorCode, errorName, errorType, failureInfo, message }: PrestoErrorObject) {
70+
super(message)
71+
this.errorCode = errorCode
72+
this.errorName = errorName
73+
this.errorType = errorType
74+
this.failureInfo = failureInfo
75+
}
76+
}

0 commit comments

Comments
 (0)