Skip to content

Commit 3d010fa

Browse files
committed
rf: Raise more issue-like objects for JSON
1 parent 3aaec8d commit 3d010fa

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

src/files/json.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Deno.test('Test JSON error conditions', async (t) => {
4040
await loadJSON(BOMfile).catch((e) => {
4141
error = e
4242
})
43-
assertObjectMatch(error, { key: 'INVALID_JSON_ENCODING' })
43+
assertObjectMatch(error, { code: 'INVALID_JSON_ENCODING' })
4444
})
4545

4646
await t.step('Error on UTF-16', async () => {
@@ -49,7 +49,7 @@ Deno.test('Test JSON error conditions', async (t) => {
4949
await loadJSON(UTF16file).catch((e) => {
5050
error = e
5151
})
52-
assertObjectMatch(error, { key: 'INVALID_JSON_ENCODING' })
52+
assertObjectMatch(error, { code: 'INVALID_JSON_ENCODING' })
5353
})
5454

5555
await t.step('Error on invalid JSON syntax', async () => {
@@ -58,6 +58,6 @@ Deno.test('Test JSON error conditions', async (t) => {
5858
await loadJSON(badJSON).catch((e) => {
5959
error = e
6060
})
61-
assertObjectMatch(error, { key: 'JSON_INVALID' })
61+
assertObjectMatch(error, { code: 'JSON_INVALID' })
6262
})
6363
})

src/files/json.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function readJSONText(file: BIDSFile): Promise<string> {
1414
}
1515
return text
1616
} catch (error) {
17-
throw { key: 'INVALID_JSON_ENCODING' }
17+
throw { code: 'INVALID_JSON_ENCODING' }
1818
} finally {
1919
decoder.decode() // Reset decoder
2020
}
@@ -26,12 +26,12 @@ export async function loadJSON(file: BIDSFile): Promise<Record<string, unknown>>
2626
try {
2727
parsedText = JSON.parse(text)
2828
} catch (error) {
29-
throw { key: 'JSON_INVALID' } // Raise syntax errors
29+
throw { code: 'JSON_INVALID' } // Raise syntax errors
3030
}
3131
if (Array.isArray(parsedText) || typeof parsedText !== 'object') {
3232
throw {
33-
key: 'JSON_NOT_AN_OBJECT',
34-
evidence: text.substring(0, 10) + (text.length > 10 ? '...' : ''),
33+
code: 'JSON_NOT_AN_OBJECT',
34+
issueMessage: text.substring(0, 10) + (text.length > 10 ? '...' : ''),
3535
}
3636
}
3737
return parsedText

src/schema/context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ export class BIDSContext implements Context {
210210
}
211211
for (const file of sidecars) {
212212
const json = await loadJSON(file).catch((error) => {
213-
if (error.key) {
214-
this.dataset.issues.add({ code: error.key, location: file.path })
213+
if (error.code) {
214+
this.dataset.issues.add({ ...error, location: file.path })
215215
return {}
216216
} else {
217217
throw error
@@ -272,8 +272,8 @@ export class BIDSContext implements Context {
272272
return
273273
}
274274
this.json = await loadJSON(this.file).catch((error) => {
275-
if (error.key) {
276-
this.dataset.issues.add({ code: error.key, location: this.file.path })
275+
if (error.code) {
276+
this.dataset.issues.add({ ...error, location: this.file.path })
277277
return {}
278278
} else {
279279
throw error

src/validators/bids.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export async function validate(
5757
const dsContext = new BIDSContextDataset({ options, schema, tree: fileTree })
5858
if (ddFile) {
5959
dsContext.dataset_description = await loadJSON(ddFile).catch((error) => {
60-
if (error.key) {
61-
dsContext.issues.add({ code: error.key, location: ddFile.path })
60+
if (error.code) {
61+
dsContext.issues.add({ ...error, location: ddFile.path })
6262
return {} as Record<string, unknown>
6363
} else {
6464
throw error

0 commit comments

Comments
 (0)