UNPKG

raygun

Version:

Raygun package for Node.js, written in TypeScript

80 lines (79 loc) 3.46 kB
/* * raygun * https://github.com/MindscapeHQ/raygun4node * * Copyright (c) 2015 MindscapeHQ * Licensed under the MIT license. */ "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendBatch = sendBatch; exports.send = send; var http_1 = __importDefault(require("http")); var https_1 = __importDefault(require("https")); var API_HOST = "api.raygun.io"; var DEFAULT_ENDPOINT = "/entries"; var BATCH_ENDPOINT = "/entries/bulk"; var debug = require("debug")("raygun"); function sendBatch(options) { return send(options, BATCH_ENDPOINT); } /** * Transport implementation that sends error to Raygun. * Errors are reported back via callback. * @param options - without callback * @param path - service endpoint * @returns Promise with IncomingMessage or rejected with Error */ function send(options, path) { var _a, _b, _c, _d; if (path === void 0) { path = DEFAULT_ENDPOINT; } try { var data_1 = Buffer.from(options.message); var httpOptions_1 = { host: ((_a = options.http) === null || _a === void 0 ? void 0 : _a.host) || API_HOST, port: ((_b = options.http) === null || _b === void 0 ? void 0 : _b.port) || 443, path: path, method: "POST", headers: { Host: ((_c = options.http) === null || _c === void 0 ? void 0 : _c.host) || API_HOST, "Content-Type": "application/json", "Content-Length": data_1.length, "X-ApiKey": (_d = options.http) === null || _d === void 0 ? void 0 : _d.apiKey, }, }; // Wrap HTTP request in Promise return new Promise(function (resolve, reject) { var _a, _b; var httpLib = ((_a = options.http) === null || _a === void 0 ? void 0 : _a.useSSL) ? https_1.default : http_1.default; var request = httpLib.request(httpOptions_1, function (response) { // request completed successfully resolve(response); // destroy the request after successful completion request.destroy(); debug("[raygun.transport.ts] Request destroyed for message: ".concat(options.message)); }); if ((_b = options.http) === null || _b === void 0 ? void 0 : _b.timeout) { debug("[raygun.transport.ts] Timeout set: ".concat(options.http.timeout, "ms")); request.setTimeout(options.http.timeout, function () { console.error("[Raygun4Node] request timed out while attempting to send error with message: ".concat(options.message)); request.destroy(new Error("Request timed out")); }); } request.on("error", function (e) { console.error("[Raygun4Node] Error with details \"".concat(e.message, "\" occurred while attempting to send error with message: ").concat(options.message)); // request failed reject(e); }); request.write(data_1); request.end(); }); } catch (e) { console.error("[Raygun4Node] Error \"".concat(e, "\" occurred while attempting to send error with message: ").concat(options.message)); return Promise.reject(e); } }