Skip to content

Commit 8545760

Browse files
copiltembeltudor
and
tudor
authored
Add error message when pg_dump fails (#562)
* gather and set text error message when throwing --------- Co-authored-by: tudor <tudor@swisstch.com>
1 parent 93a4087 commit 8545760

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

.changeset/long-swans-refuse.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@electric-sql/pglite-tools': patch
3+
---
4+
5+
pg_dump error messages set on the thrown Error

packages/pglite-tools/src/pg_dump.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async function execPgDump({
7575
}: {
7676
pg: PGlite
7777
args: string[]
78-
}): Promise<[number, Uint8Array[]]> {
78+
}): Promise<[number, Uint8Array[], string]> {
7979
const bin = new URL('./pg_dump.wasm', import.meta.url)
8080
const acc: Uint8Array[] = []
8181
const FS = emscriptenFsToWasiFS(pg.Module.FS, acc)
@@ -89,10 +89,14 @@ async function execPgDump({
8989
})
9090

9191
wasi.stdout = (_buffer) => {
92-
// console.log('stdout', buffer)
92+
// console.log('stdout', _buffer)
9393
}
94+
const textDecoder = new TextDecoder()
95+
let errorMessage = ''
96+
9497
wasi.stderr = (_buffer) => {
95-
// console.error('stderr', buffer)
98+
const text = textDecoder.decode(_buffer)
99+
if (text) errorMessage += text
96100
}
97101
wasi.sched_yield = () => {
98102
const pgIn = '/tmp/pglite/base/.s.PGSQL.5432.in'
@@ -144,7 +148,7 @@ async function execPgDump({
144148
await pg.runExclusive(async () => {
145149
exitCode = wasi.start(app.instance.exports)
146150
})
147-
return [exitCode!, acc]
151+
return [exitCode!, acc, errorMessage]
148152
}
149153

150154
interface PgDumpOptions {
@@ -173,13 +177,15 @@ export async function pgDump({
173177
'postgres',
174178
]
175179

176-
const [exitCode, acc] = await execPgDump({
180+
const [exitCode, acc, errorMessage] = await execPgDump({
177181
pg,
178182
args: [...(args ?? []), ...baseArgs],
179183
})
180184

181185
if (exitCode !== 0) {
182-
throw new Error(`pg_dump failed with exit code ${exitCode}`)
186+
throw new Error(
187+
`pg_dump failed with exit code ${exitCode}. \nError message: ${errorMessage}`,
188+
)
183189
}
184190

185191
const file = new File(acc, fileName, {

0 commit comments

Comments
 (0)