Skip to content

Commit 87534bf

Browse files
committed
added filename to JSON.parse error
1 parent f58ab8c commit 87534bf

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

commonjs/json-file.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ts/json-file.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import fs = require('fs');
88

99

1010

11-
export function readJSONFile(filename : string) : Promise<{filename: string, contents: any}> {
11+
export function readJSONFile(filename : string) : Promise<JSONFile.FilenameWContents> {
1212
return new Promise((resolve, reject) => {
1313
fs.readFile(filename, {"encoding": "utf-8"}, (error, data) => {
1414
if (error) {
@@ -18,6 +18,7 @@ export function readJSONFile(filename : string) : Promise<{filename: string, con
1818
var obj = JSON.parse(data);
1919
resolve({filename: filename, contents: obj});
2020
} catch (error) {
21+
error.message += ', in filename=' + filename
2122
reject(error);
2223
}
2324
}
@@ -44,7 +45,7 @@ function convertJSONObjOrArrayToTypedObject(json_obj : any, convertJSONObjToType
4445

4546

4647
// @arg: typename: The typename for the type in the JSON file.
47-
export function loadDatabaseFromJSONFile(filename : string, convertJSONObjToTypedObj? : (json_obj : any) => void, validate?: JSONFile.Validate, typename?: string): Promise<any> {
48+
export function loadDatabaseFromJSONFile(filename : string, convertJSONObjToTypedObj? : (json_obj : any) => void, validate?: JSONFile.Validate, typename?: string): Promise<JSONFile.FilenameWContents> {
4849
// @throws An error if the object is not valid..
4950
function checkValidity(obj) {
5051
if (Array.isArray(obj)) {

test/src/ts/test-json-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('json-file', function() {
5757
done(new Error('promise resolved'))
5858
},
5959
(error) => {
60-
expect(error.message).to.equal("Unexpected token }")
60+
expect(error.message).to.equal("Unexpected token }, in filename=" + INVALID_JSON_FILENAME)
6161
done()
6262
}
6363
)

typings/json-file/json-file.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33

44
declare module JSONFile {
5+
interface FilenameWContents {
6+
filename: string;
7+
contents: any;
8+
}
59
type Validate = (typename: string, element: any) => Error
610
}
711

@@ -11,7 +15,7 @@ declare module 'json-file' {
1115
/* Read a JSON file.
1216
* @return a Promise that resolves if the file contains valid JSON.
1317
*/
14-
function readJSONFile(filename: string): Promise<{filename: string, contents: any}>
18+
function readJSONFile(filename: string): Promise<JSONFile.FilenameWContents>
1519
/* Read a data-set from a JSON file.
1620
* @param convertJSONObjToTypedObj Optional function to convert JSON fields to equivalent objects.
1721
* For example, to convert strings to Dates.
@@ -20,5 +24,5 @@ declare module 'json-file' {
2024
* @return a Promise that resolves if the file contains valid JSON.
2125
* That is, both JSON.parse() and validate() don't find errors.
2226
*/
23-
function loadDatabaseFromJSONFile(filename: string, convertJSONObjToTypedObj?: (json_obj : any) => void, validate?: JSONFile.Validate, typename?: string): Promise<{filename: string, contents: any}>
27+
function loadDatabaseFromJSONFile(filename: string, convertJSONObjToTypedObj?: (json_obj : any) => void, validate?: JSONFile.Validate, typename?: string): Promise<JSONFile.FilenameWContents>
2428
}

0 commit comments

Comments
 (0)