Skip to content

Commit 7eb5c59

Browse files
committed
fix: error response with errors field
1 parent e27e180 commit 7eb5c59

3 files changed

Lines changed: 78 additions & 11 deletions

File tree

‎package-lock.json‎

Lines changed: 28 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/drivers/connection-ws.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ function getAttachmentLimitError(description: unknown, limit: number): SQLiteClo
5252
}
5353
}
5454

55+
function getGatewayResponseError(response: any): Record<string, any> | undefined {
56+
if (response?.error && typeof response.error === 'object') {
57+
return response.error
58+
}
59+
60+
if (Array.isArray(response?.errors) && response.errors[0] && typeof response.errors[0] === 'object') {
61+
return response.errors[0]
62+
}
63+
}
64+
5565
/**
5666
* Implementation of TransportConnection that connects to the database indirectly
5767
* via SQLite Cloud Gateway, a socket.io based deamon that responds to sql query
@@ -90,6 +100,7 @@ export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection {
90100
const raw = this.config.gatewayurl
91101
gatewayUrl = raw.startsWith('ws://') || raw.startsWith('wss://') ? raw : `ws://${raw}`
92102
ioOpts.extraHeaders = { Host: this.config.host }
103+
ioOpts.transports = ['websocket']
93104
} else {
94105
const gatewayHost = buildGatewayHost(this.config.host as string, this.config.gatewayurl)
95106
gatewayUrl = `wss://${gatewayHost}:443`
@@ -168,8 +179,9 @@ export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection {
168179
}
169180
},
170181
(response: any) => {
171-
if (response?.error) {
172-
const error = new SQLiteCloudError(response.error.detail, { ...response.error })
182+
const gatewayError = getGatewayResponseError(response)
183+
if (gatewayError) {
184+
const error = new SQLiteCloudError(gatewayError.detail || gatewayError.message || 'Gateway error', { ...gatewayError })
173185
callback?.call(this, error)
174186
} else {
175187
const { metadata } = response

‎test/connection-ws-unit.test.ts‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ describe('websocket transport helpers', () => {
2929

3030
const connection = Object.create(SQLiteCloudWebsocketConnection.prototype) as any
3131
const connectionstring = 'sqlitecloud://host.sqlite.cloud/database?apikey=secret'
32-
connection.connectTransport({ connectionstring, host: 'host.sqlite.cloud', websocketMaxAttachments: 321 }, jest.fn())
32+
connection.connectTransport({ connectionstring, host: 'host.sqlite.cloud', apikey: 'secret', websocketMaxAttachments: 321 }, jest.fn())
3333

34-
expect(mockedIo).toHaveBeenCalledWith('wss://host.sqlite.cloud:443', {
35-
auth: { token: connectionstring },
34+
expect(mockedIo).toHaveBeenCalledWith('wss://host.gateway.sqlite.cloud:443', {
35+
auth: { token: 'secret' },
3636
parser: expect.objectContaining({
3737
Encoder: expect.any(Function),
3838
Decoder: expect.any(Function)
@@ -84,6 +84,7 @@ describe('websocket transport helpers', () => {
8484
{
8585
sql: 'SELECT ?',
8686
bind: ['123n'],
87+
database: undefined,
8788
row: 'array',
8889
safe_integer_mode: 'bigint',
8990
capabilities: {
@@ -162,6 +163,38 @@ describe('websocket transport helpers', () => {
162163
})
163164
})
164165

166+
it('should read gateway errors from the errors array response field', done => {
167+
const connection = Object.create(SQLiteCloudWebsocketConnection.prototype) as any
168+
const emit = jest.fn((_event: string, _payload: any, callback: (response: any) => void) => {
169+
callback({
170+
errors: [
171+
{
172+
detail: 'Gateway validation failed.',
173+
errorCode: '40001',
174+
externalErrorCode: '0',
175+
offsetCode: -1
176+
}
177+
]
178+
})
179+
})
180+
connection.socket = { connected: true, emit }
181+
connection.config = {}
182+
183+
connection.transportCommands('SELECT 1', (error: any, results?: SQLiteCloudRowset) => {
184+
try {
185+
expect(results).toBeUndefined()
186+
expect(error).toBeDefined()
187+
expect(error.message).toBe('Gateway validation failed.')
188+
expect(error.errorCode).toBe('40001')
189+
expect(error.externalErrorCode).toBe('0')
190+
expect(error.offsetCode).toBe(-1)
191+
done()
192+
} catch (error) {
193+
done(error as Error)
194+
}
195+
})
196+
})
197+
165198
it('should surface an actionable error when socket.io attachment limits are exceeded', () => {
166199
const handlers: Record<string, (...args: any[]) => void> = {}
167200
const socket = {

0 commit comments

Comments
 (0)