Skip to content

Commit

Permalink
fix: pass ipfs config show errors (#722)
Browse files Browse the repository at this point in the history
* fix: pass ipfs config show errors

Closes #721

* fix: resolve type errors in ipfsd-daemon

Co-authored-by: Russell Dempsey <[email protected]>
  • Loading branch information
lidel and SgtPooki authored Oct 19, 2022
1 parent 17c503e commit efb2779
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/ipfsd-daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ function translateError (err: Error & { stdout: string, stderr: string }) {
return err
}

interface TranslateUnknownErrorArgs {
err: Error | unknown
stdout: string
stderr: string
nameFallback?: string
messageFallback?: string
}

function translateUnknownError ({ err, stdout, stderr, nameFallback = 'Unknown Error', messageFallback = 'Unknown Error Message' }: TranslateUnknownErrorArgs) {
const error: Error = err as Error
const name = error?.name ?? nameFallback
const message = error?.message ?? messageFallback
return translateError({
name,
message,
stdout,
stderr
})
}

/**
* Controller for daemon nodes
*/
Expand Down Expand Up @@ -353,7 +373,8 @@ class Daemon implements Controller {
}

const {
stdout
stdout,
stderr
} = await execa(
this.exec,
['config', key],
Expand All @@ -363,7 +384,17 @@ class Daemon implements Controller {
.catch(translateError)

if (key === 'show') {
return JSON.parse(stdout)
try {
return JSON.parse(stdout)
} catch (err) {
throw translateUnknownError({
err,
stderr,
stdout,
nameFallback: 'JSON.parse error',
messageFallback: 'Failed to parse stdout as JSON'
})
}
}

return stdout.trim()
Expand Down

0 comments on commit efb2779

Please sign in to comment.