Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error handling when calling json-to-go.js #143

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/node-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ jobs:
exp=$(cat tests/double-nested-objects.go)
echo "got: '${got}'"
[[ "${got}" == "${exp}" ]]

- name: Check correct error handling using stdin
shell: bash
run: |
! node json-to-go.js <<< "error"

- name: Check correct error handling with a file
shell: bash
run: |
! node json-to-go.js <(echo "error")
15 changes: 12 additions & 3 deletions json-to-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,15 @@ if (typeof module != 'undefined') {
let bigstdin = false
let filename = null

function jsonToGoWithErrorHandling(json) {
const output = jsonToGo(json)
if (output.error) {
console.error(output.error)
process.exitCode = 1
}
process.stdout.write(output.go)
}

process.argv.forEach((val, index) => {
if (index < 2)
return
Expand All @@ -519,7 +528,7 @@ if (typeof module != 'undefined') {
if (filename) {
const fs = require('fs');
const json = fs.readFileSync(filename, 'utf8');
process.stdout.write(jsonToGo(json).go)
jsonToGoWithErrorHandling(json)
return
}

Expand All @@ -530,15 +539,15 @@ if (typeof module != 'undefined') {
})
process.stdin.on('end', function() {
const json = Buffer.concat(bufs).toString('utf8')
process.stdout.write(jsonToGo(json).go)
jsonToGoWithErrorHandling(json)
})
return
}

// read from stdin
process.stdin.on('data', function(buf) {
const json = buf.toString('utf8')
process.stdout.write(jsonToGo(json).go)
jsonToGoWithErrorHandling(json)
})
} else {
module.exports = jsonToGo
Expand Down