Skip to content

Commit

Permalink
Merge pull request #7 from placemark/json-errors
Browse files Browse the repository at this point in the history
Handle JSON errors
  • Loading branch information
tmcw authored Jun 26, 2021
2 parents 9e6363b + 6f408ab commit e64d3ce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ interface HintIssueType extends HintIssueBase {
code: 'invalid_type';
}

export type HintIssue = HintIssueRootType | HintIssueType;
interface HintJSONIssue {
code: 'invalid_json';
line: number;
}

export type HintIssue = HintIssueRootType | HintIssueType | HintJSONIssue;

export class HintError extends Error {
issues: HintIssue[] = [];
Expand Down
15 changes: 2 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,8 @@ export const check = (jsonStr: string): GeoJSON => {
});
} catch (e) {
issues.push({
code: 'invalid_type',
loc: {
start: {
line: e.line,
column: e.column,
offset: 0,
},
end: {
line: e.line,
column: e.column,
offset: 0,
},
},
code: 'invalid_json',
line: e.line,
});
}
if (ast) checkObject(issues, ast.body);
Expand Down
16 changes: 16 additions & 0 deletions test/bad.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ describe('check', () => {
).toThrow(HintError);
});

it('bad json', () => {
try {
check(
`{
"type": "MultiPoint"
"coordinates": [["foo", "bar"]]
}`
);
} catch (e) {
expect(e.issues[0]).toEqual({
code: 'invalid_json',
line: 3,
});
}
});

describe('works with fixtures', () => {
const fixtureNames = readdirSync(Path.join(__dirname, './fixture/bad/'));
for (let name of fixtureNames) {
Expand Down

0 comments on commit e64d3ce

Please sign in to comment.