forked from mateodelnorte/servicebus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mateodelnorte#77 - handling bad JSON
- Loading branch information
Alex Kazantsev
committed
May 11, 2017
1 parent
d9d929a
commit c148566
Showing
3 changed files
with
95 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,30 @@ | ||
module.exports.deserialize = function deserialize (content) { | ||
|
||
module.exports.deserialize = function deserialize(content, cb) { | ||
|
||
if (content === null || content === undefined) return cb(null, content); | ||
|
||
/** | ||
* Return input string | ||
*/ | ||
try { | ||
content = JSON.parse(content); | ||
} catch (err) { | ||
throw err; | ||
console.log('Content - is not valid JSON: ', err); | ||
return cb(err); | ||
} | ||
|
||
return content; | ||
cb(null, content); | ||
|
||
}; | ||
|
||
module.exports.serialize = function serialize (content) { | ||
module.exports.serialize = function serialize(content, cb) { | ||
|
||
try { | ||
content = JSON.stringify(content); | ||
} catch (err) { | ||
throw err; | ||
console.log('Content can\'t serialized to JSON format: ', err); | ||
return cb(err); | ||
} | ||
|
||
return content; | ||
cb(null, content); | ||
|
||
}; |
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