logflare-transport-core
Advanced tools
Comparing version 0.3.0-88073b96 to 0.3.0-c91dc868
@@ -11,2 +11,5 @@ interface IngestTransformsI { | ||
fromBrowser?: boolean; | ||
onError?: ((payload: { | ||
batch: object[]; | ||
}, err: Error) => void) | undefined; | ||
} | ||
@@ -20,2 +23,8 @@ declare class LogflareHttpClient { | ||
protected readonly apiBaseUrl: string; | ||
/** | ||
* onError takes in an optional callback function to handle any errors returned by logflare | ||
*/ | ||
protected readonly onError?: ((payload: { | ||
batch: object[]; | ||
}, err: Error) => void) | undefined; | ||
constructor(options: LogflareUserOptionsI); | ||
@@ -22,0 +31,0 @@ addLogEvent(logEvent: object | object[]): Promise<object>; |
@@ -85,2 +85,3 @@ "use strict"; | ||
this.apiBaseUrl = options.apiBaseUrl || defaultOptions.apiBaseUrl; | ||
this.onError = options.onError; | ||
} | ||
@@ -97,6 +98,7 @@ LogflareHttpClient.prototype.addLogEvent = function (logEvent) { | ||
LogflareHttpClient.prototype.postLogEvents = function (batch) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var path, payload, url, response, data, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
@@ -112,5 +114,5 @@ if (this.endpoint === "typecasting") { | ||
}; | ||
_a.label = 1; | ||
_b.label = 1; | ||
case 1: | ||
_a.trys.push([1, 4, , 5]); | ||
_b.trys.push([1, 4, , 5]); | ||
url = new URL(path, this.apiBaseUrl); | ||
@@ -126,6 +128,6 @@ return [4 /*yield*/, fetch(url.toString(), { | ||
case 2: | ||
response = _a.sent(); | ||
response = _b.sent(); | ||
return [4 /*yield*/, response.json()]; | ||
case 3: | ||
data = _a.sent(); | ||
data = _b.sent(); | ||
if (!response.ok) { | ||
@@ -136,10 +138,11 @@ throw new NetworkError("Network response was not ok for \"".concat(url, "\""), response, data); | ||
case 4: | ||
e_1 = _a.sent(); | ||
if (e_1) { | ||
e_1 = _b.sent(); | ||
if (e_1 && e_1 instanceof Error) { | ||
if (e_1 instanceof NetworkError && e_1.response) { | ||
console.error("Logflare API request failed with ".concat(e_1.response.status, " status: ").concat(JSON.stringify(e_1.data))); | ||
} | ||
else if (e_1 instanceof Error) { | ||
else { | ||
console.error(e_1.message); | ||
} | ||
(_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, payload, e_1); | ||
} | ||
@@ -146,0 +149,0 @@ return [2 /*return*/, e_1]; |
{ | ||
"name": "logflare-transport-core", | ||
"version": "0.3.0-88073b96", | ||
"version": "0.3.0-c91dc868", | ||
"description": "A common core for Logflare javascript transports.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -12,2 +12,10 @@ interface IngestTransformsI { | ||
fromBrowser?: boolean | ||
onError?: | ||
| (( | ||
payload: { | ||
batch: object[] | ||
}, | ||
err: Error | ||
) => void) | ||
| undefined | ||
} | ||
@@ -38,2 +46,13 @@ | ||
protected readonly apiBaseUrl: string | ||
/** | ||
* onError takes in an optional callback function to handle any errors returned by logflare | ||
*/ | ||
protected readonly onError?: | ||
| (( | ||
payload: { | ||
batch: object[] | ||
}, | ||
err: Error | ||
) => void) | ||
| undefined | ||
@@ -54,2 +73,3 @@ public constructor(options: LogflareUserOptionsI) { | ||
this.apiBaseUrl = options.apiBaseUrl || defaultOptions.apiBaseUrl | ||
this.onError = options.onError | ||
} | ||
@@ -96,3 +116,3 @@ | ||
} catch (e) { | ||
if (e) { | ||
if (e && e instanceof Error) { | ||
if (e instanceof NetworkError && e.response) { | ||
@@ -104,5 +124,6 @@ console.error( | ||
) | ||
} else if (e instanceof Error) { | ||
} else { | ||
console.error(e.message) | ||
} | ||
this.onError?.(payload, e) | ||
} | ||
@@ -109,0 +130,0 @@ |
@@ -10,6 +10,14 @@ import {LogflareHttpClient} from "./main" | ||
let httpClient | ||
let httpClientOnError | ||
let nativeFetch | ||
let consoleErrorData = [] | ||
const storeLog = (inputs) => consoleErrorData.push(inputs) | ||
const onErrorCallback = jest.fn((payload, err) => { | ||
console.error(payload) | ||
console.error(err) | ||
}) | ||
beforeAll(() => { | ||
nativeFetch = global.fetch | ||
console["error"] = jest.fn(storeLog) | ||
}) | ||
@@ -23,2 +31,8 @@ | ||
}) | ||
httpClientOnError = new LogflareHttpClient({ | ||
apiKey: testApiKey, | ||
sourceToken: testSourceToken, | ||
apiBaseUrl: "http://non-existing.domain", | ||
onError: onErrorCallback, | ||
}) | ||
}) | ||
@@ -28,2 +42,3 @@ | ||
global.fetch.mockClear() | ||
consoleErrorData = [] | ||
}) | ||
@@ -61,8 +76,5 @@ | ||
let consoleLogData = "" | ||
const storeLog = (inputs) => (consoleLogData += inputs) | ||
it("prints to console on error", async () => { | ||
const errorResponse = {message: "Schema validation error"} | ||
console["error"] = jest.fn(storeLog) | ||
global.fetch = jest.fn(() => | ||
@@ -79,3 +91,3 @@ Promise.resolve({ | ||
await httpClient.addLogEvent(le) | ||
expect(consoleLogData).toBe( | ||
expect(consoleErrorData[0]).toBe( | ||
`Logflare API request failed with 406 status: ${JSON.stringify( | ||
@@ -86,2 +98,30 @@ errorResponse | ||
}) | ||
it("invoke onError callback on error", async () => { | ||
const errorResponse = {message: "Schema validation error"} | ||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
json: () => Promise.resolve(errorResponse), | ||
ok: false, | ||
status: 406, | ||
}) | ||
) | ||
const le = {message: "info log msg", metadata: {p1: "v1"}} | ||
await httpClientOnError.addLogEvent(le) | ||
const [message, payload, err] = consoleErrorData | ||
expect(message).toBe( | ||
`Logflare API request failed with 406 status: ${JSON.stringify( | ||
errorResponse | ||
)}` | ||
) | ||
expect(onErrorCallback).toHaveBeenCalledTimes(1) | ||
expect(payload).toStrictEqual({ | ||
batch: [le], | ||
}) | ||
expect(err.message).toMatch(/Network response was not ok for/) | ||
expect(err.data).toBe(errorResponse) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29453
15
535