@hkdigital/lib-core 0.3.7 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,32 +15,44 @@ export class PinoAdapter {
15
15
  * @param {Object} [options] - Pino configuration options
16
16
  */
17
17
  constructor(options = {}) {
18
- const defaultOptions = dev
19
- ? {
20
- level: 'debug',
21
- serializers: {
22
- err: (err) => {
23
- const chain = [];
24
- let current = err;
25
- let isFirst = true;
18
+ const baseOptions = {
19
+ serializers: {
20
+ err: (err) => {
21
+ const chain = [];
22
+ let current = err;
23
+ let isFirst = true;
26
24
 
27
- while (current) {
28
- const serialized = {
29
- name: current.name,
30
- message: current.message,
31
- ...(isFirst &&
32
- this.pino.level === 'debug' && {
33
- stack: current.stack
34
- })
35
- };
36
- chain.push(serialized);
37
- current = current.cause;
38
- isFirst = false;
39
- }
25
+ while (current) {
26
+ const serialized = {
27
+ name: current.name,
28
+ message: current.message,
29
+ ...(isFirst &&
30
+ this.pino.level === 'debug' && {
31
+ stack: current.stack
32
+ })
33
+ };
40
34
 
41
- return { errorChain: chain };
35
+ // Include HttpError-specific properties
36
+ if (current.status !== undefined) {
37
+ serialized.status = current.status;
42
38
  }
43
- },
39
+ if (current.details !== undefined) {
40
+ serialized.details = current.details;
41
+ }
42
+
43
+ chain.push(serialized);
44
+ current = current.cause;
45
+ isFirst = false;
46
+ }
47
+
48
+ return { errorChain: chain };
49
+ }
50
+ }
51
+ };
52
+
53
+ const devOptions = dev
54
+ ? {
55
+ level: 'debug',
44
56
  transport: {
45
57
  target: 'pino-pretty',
46
58
  options: {
@@ -50,7 +62,7 @@ export class PinoAdapter {
50
62
  }
51
63
  : {};
52
64
 
53
- this.pino = pino({ ...defaultOptions, ...options });
65
+ this.pino = pino({ ...baseOptions, ...devOptions, ...options });
54
66
  }
55
67
 
56
68
  /**
@@ -63,10 +75,28 @@ export class PinoAdapter {
63
75
 
64
76
  const logData = {
65
77
  source,
66
- timestamp,
67
- ...(details && { details })
78
+ timestamp
68
79
  };
69
80
 
81
+ // Check if details contains an error and promote it to err property for pino serializer
82
+ if (details) {
83
+ if (details instanceof Error) {
84
+ // details is directly an error
85
+ logData.err = details;
86
+ } else if (details.error instanceof Error) {
87
+ // details has an error property
88
+ logData.err = details.error;
89
+ // Include other details except the error
90
+ const { error, ...otherDetails } = details;
91
+ if (Object.keys(otherDetails).length > 0) {
92
+ logData.details = otherDetails;
93
+ }
94
+ } else {
95
+ // No error found in details, include all details
96
+ logData.details = details;
97
+ }
98
+ }
99
+
70
100
  this.pino[level](logData, message);
71
101
  }
72
102
 
@@ -1,4 +1,4 @@
1
- import { ResponseError } from '../../errors/index.js';
1
+ import { ResponseError, HttpError } from '../../errors/index.js';
2
2
  import * as expect from '../expect/index.js';
3
3
  import { toURL } from './url.js';
4
4
 
@@ -155,11 +155,35 @@ export async function waitForAndCheckResponse(responsePromise, url) {
155
155
  response = await responsePromise;
156
156
 
157
157
  if (response && false === response.ok) {
158
- // if response.ok is false, it also indicates a network error
159
- throw new Error(`Response failed [response.ok=false]`);
158
+ // Check if this is a network error (status 0) vs HTTP error
159
+ if (response.status === 0) {
160
+ // Network error - treat as before
161
+ throw new Error(`Response failed [response.ok=false]`);
162
+ }
163
+
164
+ // HTTP error - get response body for detailed error information
165
+ const responseBody = await response.text();
166
+ let errorDetails;
167
+
168
+ try {
169
+ // Try to parse as JSON (common for API errors)
170
+ errorDetails = JSON.parse(responseBody);
171
+ } catch {
172
+ // Fallback to plain text
173
+ errorDetails = responseBody;
174
+ }
175
+
176
+ throw new HttpError(
177
+ response.status,
178
+ `HTTP ${response.status}: ${response.statusText}`,
179
+ errorDetails
180
+ );
160
181
  }
161
182
  } catch (e) {
162
- if (e instanceof TypeError || response?.ok === false) {
183
+ if (e instanceof HttpError) {
184
+ // Re-throw HttpError as-is
185
+ throw e;
186
+ } else if (e instanceof TypeError || response?.ok === false) {
163
187
  throw new ResponseError(
164
188
  `A network error occurred for request [${href(url)}]`,
165
189
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"