UNPKG

bugsplat

Version:
82 lines (81 loc) 3.08 kB
import fetchPonyfill from "fetch-ponyfill"; import FormData from 'form-data'; export class BugSplat { constructor(_database, _appName, _appVersion) { this._database = _database; this._appName = _appName; this._appVersion = _appVersion; this._fetch = fetchPonyfill().fetch; this._formData = () => new FormData(); this._appKey = ''; this._description = ''; this._email = ''; this._user = ''; } async post(errorToPost, options) { options = options || {}; const appKey = options.appKey || this._appKey; const user = options.user || this._user; const email = options.email || this._email; const description = options.description || this._description; const additionalFormDataParams = options.additionalFormDataParams || []; const url = "https://" + this._database + ".bugsplat.com/post/js/"; const callstack = !errorToPost.stack ? `${errorToPost}` : errorToPost.stack; const method = "POST"; const body = this._formData(); body.append("database", this._database); body.append("appName", this._appName); body.append("appVersion", this._appVersion); body.append("appKey", appKey); body.append("user", user); body.append("email", email); body.append("description", description); body.append("callstack", callstack); additionalFormDataParams.forEach(param => body.append(param.key, param.value)); console.log("BugSplat Error:", errorToPost); console.log("BugSplat Url:", url); const response = await this._fetch(url, { method, body }); const json = await this._tryParseResponseJson(response); console.log("BugSplat POST status code:", response.status); console.log("BugSplat POST response body:", json); if (response.status === 400) { return this._createReturnValue(new Error("BugSplat Error: Bad request"), json, errorToPost); } if (response.status === 429) { return this._createReturnValue(new Error("BugSplat Error: Rate limit of one crash per second exceeded"), json, errorToPost); } if (!response.ok) { return this._createReturnValue(new Error("BugSplat Error: Unknown error"), json, errorToPost); } return this._createReturnValue(null, json, errorToPost); } setDefaultAppKey(appKey) { this._appKey = appKey; } setDefaultDescription(description) { this._description = description; } setDefaultEmail(email) { this._email = email; } setDefaultUser(user) { this._user = user; } _createReturnValue(error, response, original) { return { error, response, original }; } async _tryParseResponseJson(response) { let parsed; try { parsed = await response.json(); } catch (_) { parsed = {}; } return parsed; } }