logflare-transport-core
Advanced tools
Comparing version 0.3.0-d4e039ee to 0.3.0-e951c226
{ | ||
"name": "logflare-transport-core", | ||
"version": "0.3.0-d4e039ee", | ||
"version": "0.3.0-e951c226", | ||
"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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
0
16582
10
327