From 3906b27e0a8ce9bc1e6d54ee52fc65590c58c4f2 Mon Sep 17 00:00:00 2001 From: pluralcafe-docker Date: Wed, 22 Aug 2018 07:10:06 +0000 Subject: Use insecure mode for docker --- node-mastodon/lib/helpers.js | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 node-mastodon/lib/helpers.js (limited to 'node-mastodon/lib/helpers.js') diff --git a/node-mastodon/lib/helpers.js b/node-mastodon/lib/helpers.js new file mode 100644 index 0000000..8833968 --- /dev/null +++ b/node-mastodon/lib/helpers.js @@ -0,0 +1,65 @@ +var querystring = require('querystring'); +var request = require('request'); + +/** + * For each `/:param` fragment in path, move the value in params + * at that key to path. If the key is not found in params, throw. + * Modifies both params and path values. + * + * @param {Objet} params Object used to build path. + * @param {String} path String to transform. + * @return {Undefined} + * + */ +exports.moveParamsIntoPath = function (params, path) { + var rgxParam = /\/:(\w+)/g + var missingParamErr = null + + path = path.replace(rgxParam, function (hit) { + var paramName = hit.slice(2) + var suppliedVal = params[paramName] + if (!suppliedVal) { + throw new Error('Mastodon: Params object is missing a required parameter for this request: `'+paramName+'`') + } + var retVal = '/' + suppliedVal + delete params[paramName] + return retVal + }) + return path +} + +/** + * When Mastodon returns a response that looks like an error response, + * use this function to attach the error info in the response body to `err`. + * + * @param {Error} err Error instance to which body info will be attached + * @param {Object} body JSON object that is the deserialized HTTP response body received from Mastodon + * @return {Undefined} + */ +exports.attachBodyInfoToError = function (err, body) { + err.mastodonReply = body; + if (!body) { + return + } + if (body.error) { + // the body itself is an error object + err.message = body.error + err.allErrors = err.allErrors.concat([body]) + } else if (body.errors && body.errors.length) { + // body contains multiple error objects + err.message = body.errors[0].message; + err.code = body.errors[0].code; + err.allErrors = err.allErrors.concat(body.errors) + } +} + +exports.makeMastodonError = function (message) { + var err = new Error() + if (message) { + err.message = message + } + err.code = null + err.allErrors = [] + err.mastodonReply = null + return err +} -- cgit