Skip to content

Commit 5567366

Browse files
author
Daniele Briggi
committed
fix(ws): error message
1 parent 2cfc907 commit 5567366

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.653",
3+
"version": "1.0.715",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/connection-ws.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection {
4444

4545
this.socket.on('connect_error', (error: any) => {
4646
this.close()
47-
callback?.call(
48-
this,
49-
new SQLiteCloudError(JSON.parse(error.context.responseText).message || 'Connection error', { errorCode: 'ERR_CONNECTION_ERROR' })
50-
)
47+
let message = error.message || 'Connection error'
48+
if (typeof error.context == 'object' && error.context.responseText) {
49+
message = typeof error.context.responseText == 'string' ? error.context.responseText : JSON.parse(error.context.responseText)?.message
50+
}
51+
callback?.call(this, new SQLiteCloudError(message, { errorCode: 'ERR_CONNECTION_ERROR' }))
5152
})
5253

5354
this.socket.on('error', (error: Error) => {

test/connection-ws.test.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ import { SQLiteCloudConnection } from '../src/drivers/connection'
66
import { SQLiteCloudWebsocketConnection } from '../src/drivers/connection-ws'
77
import { SQLiteCloudCommand } from '../src/drivers/types'
88
import { SQLiteCloudError } from '../src/index'
9-
import {
10-
EXPECT_SPEED_MS,
11-
getChinookConfig,
12-
getChinookWebsocketConnection,
13-
LONG_TIMEOUT,
14-
WARN_SPEED_MS
15-
} from './shared'
9+
import { EXPECT_SPEED_MS, getChinookConfig, getChinookWebsocketConnection, LONG_TIMEOUT, WARN_SPEED_MS } from './shared'
1610

1711
describe('connection-ws', () => {
1812
let chinook: SQLiteCloudConnection
@@ -78,6 +72,27 @@ describe('connection-ws', () => {
7872
})
7973
})
8074
*/
75+
76+
it(
77+
'should catch connect_error on connection errors',
78+
done => {
79+
const configObj = getChinookConfig()
80+
configObj.usewebsocket = true
81+
configObj.host = 'non.existing.host.name'
82+
let connection: SQLiteCloudWebsocketConnection | null = null
83+
connection = new SQLiteCloudWebsocketConnection(configObj, error => {
84+
try {
85+
expect(error).toBeDefined()
86+
expect(error?.message.startsWith('Error: getaddrinfo ENOTFOUND non.existing.host.name')).toBe(true)
87+
connection?.close()
88+
} finally {
89+
done()
90+
}
91+
})
92+
},
93+
LONG_TIMEOUT
94+
)
95+
8196
describe('send test commands', () => {
8297
it('should test integer', done => {
8398
chinook.sendCommands('TEST INTEGER', (error, results) => {

0 commit comments

Comments
 (0)