mirror of
https://github.com/badges/shields.git
synced 2026-11-21 16:06:35 +03:00
Signed-off-by: Gautam Kumar <gautamkumarofficial@users.noreply.github.com> Co-authored-by: Gautam Kumar <gautamkumarofficial@users.noreply.github.com>
32 lines
848 B
JavaScript
32 lines
848 B
JavaScript
// See available emoji at http://emoji.muan.co/
|
|
import emojic from 'emojic'
|
|
import { InvalidResponse } from './errors.js'
|
|
import trace from './trace.js'
|
|
|
|
/**
|
|
* Parse a JSON response buffer. Throws an `InvalidResponse` error when the
|
|
* JSON is unparseable.
|
|
*
|
|
* @param {string|Buffer} buffer - The raw response body.
|
|
* @returns {object|Array} The parsed JSON value.
|
|
*/
|
|
function parseJson(buffer) {
|
|
const logTrace = (...args) => trace.logTrace('fetch', ...args)
|
|
let json
|
|
try {
|
|
json = JSON.parse(buffer)
|
|
} catch (err) {
|
|
logTrace(emojic.dart, 'Response JSON (unparseable)', buffer)
|
|
throw new InvalidResponse({
|
|
prettyMessage: 'unparseable json response',
|
|
underlyingError: err,
|
|
})
|
|
}
|
|
logTrace(emojic.dart, 'Response JSON (before validation)', json, {
|
|
deep: true,
|
|
})
|
|
return json
|
|
}
|
|
|
|
export { parseJson }
|