File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 3
3
*/
4
4
'use strict' ;
5
5
6
+ const JsonLdError = require ( './JsonLdError' ) ;
6
7
const graphTypes = require ( './graphTypes' ) ;
7
8
const types = require ( './types' ) ;
8
9
const util = require ( './util' ) ;
@@ -288,7 +289,14 @@ function _RDFToObject(o, useNativeTypes) {
288
289
}
289
290
if ( type === RDF_JSON_LITERAL ) {
290
291
type = '@json' ;
291
- rval [ '@value' ] = JSON . parse ( rval [ '@value' ] ) ;
292
+ try {
293
+ rval [ '@value' ] = JSON . parse ( rval [ '@value' ] ) ;
294
+ } catch ( e ) {
295
+ throw new JsonLdError (
296
+ 'JSON literal could not be parsed.' ,
297
+ 'jsonld.InvalidJsonLiteral' ,
298
+ { code : 'invalid JSON literal' , value : rval [ '@value' ] , cause : e } ) ;
299
+ }
292
300
}
293
301
// use native types for certain xsd types
294
302
if ( useNativeTypes ) {
Original file line number Diff line number Diff line change @@ -519,3 +519,20 @@ describe('js keywords', () => {
519
519
assert . deepStrictEqual ( e , ex ) ;
520
520
} ) ;
521
521
} ) ;
522
+
523
+ describe . only ( 'literal JSON' , ( ) => {
524
+ it ( 'handles error' , done => {
525
+ const d =
526
+ '_:b0 <ex:p> "bogus"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON> .'
527
+ ;
528
+ const p = jsonld . fromRDF ( d ) ;
529
+ assert ( p instanceof Promise ) ;
530
+ p . then ( ( ) => {
531
+ assert . fail ( ) ;
532
+ } ) . catch ( e => {
533
+ assert ( e ) ;
534
+ assert . equal ( e . name , 'jsonld.InvalidJsonLiteral' ) ;
535
+ done ( ) ;
536
+ } ) ;
537
+ } ) ;
538
+ } ) ;
You can’t perform that action at this time.
0 commit comments