-
Notifications
You must be signed in to change notification settings - Fork 60
Description
Details
By the docs, the user needs to pass only host
as an url
option, so the exact URL is being built internally here:
https://github.com/JaniAnttonen/winston-loki/blob/development/src/batcher.js#L42-L43
But if the user doesn't follow docs, or makes a typo, and passes not only a host but for example, a full URL, then internal implementation doesn't care about it and builds an invalid URL. Then, in the req.post
, an invalid URL is being silently processed with no error, so in fact, no logs are being sent, and no error is being thrown. Here's the problem:
https://github.com/JaniAnttonen/winston-loki/blob/development/src/batcher.js#L257-L272
Inside then
, the response
argument should be handled, and if something bad happens, it should throw an error so that catch
can handle it correctly. For example, for an invalid URL the response
will have a text error saying 404 URL not found
.
Branch
No response
Improve Error Handling for Invalid URLs in Winston Loki Transport
Description:
Enhance error handling in the Batcher class to properly handle and report invalid URLs and HTTP response errors, particularly when users provide incorrect URL formats.
Tasks:
-
In
src/batcher.js
, update URL handling:- Add validation for the
host
parameter in constructor - Ensure URL construction handles both host-only and full URL inputs
- Add error handling for malformed URLs
- Add validation for the
-
In
src/batcher.js
, enhancesendBatchToLoki
method:- Add response status code validation
- Throw appropriate errors for non-200 responses
- Include response body in error messages
Test:
-
In
test/batcher.json.test.js
, add tests:- Invalid host parameter
- Full URL in host parameter
- Non-200 HTTP responses
- Response error messages
-
In
test/requests.test.js
, add tests:- HTTP error responses
- Invalid URL handling
- Response body parsing
Additional Context:
The changes focus on the URL construction in the Batcher constructor and HTTP response handling in the sendBatchToLoki
method to prevent silent failures when logs aren't being sent.