-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
loader-utils: Improved JSON parsding error messages
- Loading branch information
Ib Green
committed
Sep 23, 2019
1 parent
bc5f216
commit bb4435e
Showing
6 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export function getFirstCharacters(data, length = 5) { | ||
if (typeof data === 'string') { | ||
return data.slice(0, length); | ||
} else if (ArrayBuffer.isView(data)) { | ||
// Typed Arrays can have offsets into underlying buffer | ||
return getMagicString(data.buffer, data.byteOffset, length); | ||
} else if (data instanceof ArrayBuffer) { | ||
const byteOffset = 0; | ||
return getMagicString(data, byteOffset, length); | ||
} | ||
return ''; | ||
} | ||
|
||
export function getMagicString(arrayBuffer, byteOffset, length) { | ||
if (arrayBuffer.byteLength <= byteOffset + length) { | ||
return ''; | ||
} | ||
const dataView = new DataView(arrayBuffer); | ||
let magic = ''; | ||
for (let i = 0; i < length; i++) { | ||
magic += String.fromCharCode(dataView.getUint8(byteOffset + i)); | ||
} | ||
return magic; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {getFirstCharacters} from './get-first-characters'; | ||
|
||
// Minimal JSON parser with a meaningful error message | ||
export function parseJSON(string) { | ||
try { | ||
return JSON.parse(string); | ||
} catch (_) { | ||
throw new Error(`Failed to parse JSON from data starting with "${getFirstCharacters(string)}"`); | ||
} | ||
} |