Skip to content

Commit

Permalink
refactor: Handle more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jan 16, 2025
1 parent f4ddce0 commit 0739181
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions xmcl-electron-app/main/ElectronLauncherApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export default class ElectronLauncherApp extends LauncherApp {
code = NetworkErrorCode.NETWORK_CHANGED
} else if (e.message === 'net::PROXY_CONNECTION_FAILED') {
code = NetworkErrorCode.PROXY_CONNECTION_FAILED
} else if (e.message === 'net::ERR_UNEXPECTED') {
code = NetworkErrorCode.CONNECTION_RESET
}
if (code) {
// expected exceptions
Expand Down
8 changes: 8 additions & 0 deletions xmcl-runtime/service/ServiceStateContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,21 @@ export class ServiceStateContainer<T = any> implements ServiceStateContext {
try {
this.semaphore += 1
for (const [c] of this.#clients) {
if (c.isDestroyed()) {
this.untrack(c)
continue
}
c.send('state-validating', { id: this.id, semaphore: this.semaphore })
}
return await action
} finally {
this.semaphore -= 1
if (this.semaphore === 0) {
for (const [c] of this.#clients) {
if (c.isDestroyed()) {
this.untrack(c)
continue
}
c.send('state-validating', { id: this.id, semaphore: this.semaphore })
}
}
Expand Down
15 changes: 14 additions & 1 deletion xmcl-runtime/sql/SqliteWASMDriver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CompiledQuery, DatabaseConnection, Driver, QueryResult, SelectQueryNode } from 'kysely'
import type { Database } from 'node-sqlite3-wasm'
import { SQLite3Error } from 'node-sqlite3-wasm'
import { SqliteWASMDialectDatabaseConfig, SqliteWASMDialectWorkerConfig } from './SqliteWASMDialectConfig'
import { Exception } from '@xmcl/runtime-api'

declare module 'node-sqlite3-wasm' {
interface Statement {
Expand Down Expand Up @@ -56,7 +58,7 @@ export class SqliteWASMDriver extends AbstractSqliteDriver {
readonly #config: SqliteWASMDialectDatabaseConfig

#db?: Database
#connection?: DatabaseConnection
#connection?: SqliteConnection

constructor(config: SqliteWASMDialectDatabaseConfig) {
super()
Expand All @@ -76,17 +78,23 @@ export class SqliteWASMDriver extends AbstractSqliteDriver {
}

async destroy(): Promise<void> {
this.#connection?.dispose()
this.#db?.close()
}
}

class SqliteConnection implements DatabaseConnection {
readonly #db: Database
#disposed = false

constructor(db: Database) {
this.#db = db
}

dispose() {
this.#disposed = true
}

executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
const { sql, parameters } = compiledQuery
const stmt = this.#db.prepare(sql)
Expand All @@ -110,6 +118,11 @@ class SqliteConnection implements DatabaseConnection {
: undefined,
rows: [],
})
} catch (e) {
if (this.#disposed && e instanceof SQLite3Error) {
return Promise.reject(new Exception({ type: 'sqlite3Exception' }, e.message, { cause: e }))
}
return Promise.reject(e)
} finally {
stmt.finalize()
}
Expand Down

0 comments on commit 0739181

Please sign in to comment.