Skip to content

Commit 0739181

Browse files
committed
refactor: Handle more errors
1 parent f4ddce0 commit 0739181

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

xmcl-electron-app/main/ElectronLauncherApp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ export default class ElectronLauncherApp extends LauncherApp {
131131
code = NetworkErrorCode.NETWORK_CHANGED
132132
} else if (e.message === 'net::PROXY_CONNECTION_FAILED') {
133133
code = NetworkErrorCode.PROXY_CONNECTION_FAILED
134+
} else if (e.message === 'net::ERR_UNEXPECTED') {
135+
code = NetworkErrorCode.CONNECTION_RESET
134136
}
135137
if (code) {
136138
// expected exceptions

xmcl-runtime/service/ServiceStateContainer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,21 @@ export class ServiceStateContainer<T = any> implements ServiceStateContext {
8888
try {
8989
this.semaphore += 1
9090
for (const [c] of this.#clients) {
91+
if (c.isDestroyed()) {
92+
this.untrack(c)
93+
continue
94+
}
9195
c.send('state-validating', { id: this.id, semaphore: this.semaphore })
9296
}
9397
return await action
9498
} finally {
9599
this.semaphore -= 1
96100
if (this.semaphore === 0) {
97101
for (const [c] of this.#clients) {
102+
if (c.isDestroyed()) {
103+
this.untrack(c)
104+
continue
105+
}
98106
c.send('state-validating', { id: this.id, semaphore: this.semaphore })
99107
}
100108
}

xmcl-runtime/sql/SqliteWASMDriver.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { CompiledQuery, DatabaseConnection, Driver, QueryResult, SelectQueryNode } from 'kysely'
22
import type { Database } from 'node-sqlite3-wasm'
3+
import { SQLite3Error } from 'node-sqlite3-wasm'
34
import { SqliteWASMDialectDatabaseConfig, SqliteWASMDialectWorkerConfig } from './SqliteWASMDialectConfig'
5+
import { Exception } from '@xmcl/runtime-api'
46

57
declare module 'node-sqlite3-wasm' {
68
interface Statement {
@@ -56,7 +58,7 @@ export class SqliteWASMDriver extends AbstractSqliteDriver {
5658
readonly #config: SqliteWASMDialectDatabaseConfig
5759

5860
#db?: Database
59-
#connection?: DatabaseConnection
61+
#connection?: SqliteConnection
6062

6163
constructor(config: SqliteWASMDialectDatabaseConfig) {
6264
super()
@@ -76,17 +78,23 @@ export class SqliteWASMDriver extends AbstractSqliteDriver {
7678
}
7779

7880
async destroy(): Promise<void> {
81+
this.#connection?.dispose()
7982
this.#db?.close()
8083
}
8184
}
8285

8386
class SqliteConnection implements DatabaseConnection {
8487
readonly #db: Database
88+
#disposed = false
8589

8690
constructor(db: Database) {
8791
this.#db = db
8892
}
8993

94+
dispose() {
95+
this.#disposed = true
96+
}
97+
9098
executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
9199
const { sql, parameters } = compiledQuery
92100
const stmt = this.#db.prepare(sql)
@@ -110,6 +118,11 @@ class SqliteConnection implements DatabaseConnection {
110118
: undefined,
111119
rows: [],
112120
})
121+
} catch (e) {
122+
if (this.#disposed && e instanceof SQLite3Error) {
123+
return Promise.reject(new Exception({ type: 'sqlite3Exception' }, e.message, { cause: e }))
124+
}
125+
return Promise.reject(e)
113126
} finally {
114127
stmt.finalize()
115128
}

0 commit comments

Comments
 (0)