@digitraffic/common 2023.3.21-2 → 2023.4.3-1

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.
@@ -4,38 +4,72 @@ import { getEnvVariableOrElse } from "./utils";
4
4
 
5
5
  const functionName = getEnvVariableOrElse("AWS_LAMBDA_FUNCTION_NAME", "test");
6
6
 
7
- export function logException(
8
- logger: DtLogger,
9
- error: Error | string,
10
- includeStack?: boolean
11
- ): void;
12
- export function logException(logger: DtLogger, error: AxiosError): void;
7
+ /**
8
+ * Curried version of logException.
9
+ *
10
+ * @example <caption>Using default configuration</caption>
11
+ * Promise.reject(x).catch(createExceptionLogger())
12
+ *
13
+ * @example <caption>Providing external logger and requiring stack</caption>
14
+ * import {logger} from "@digitraffic/common/dist/aws/runtime/dt-logger-default"
15
+ * Promise.reject(x).catch(createExceptionLogger(logger, true))
16
+ *
17
+ * @param [logger=undefined] - DtLogger to use. If not given, will create a new instance of DtLogger
18
+ * @param [includeStack=false] - Define if the stack trace should be logged.
19
+ * @param error - The error instance to be logged.
20
+ * @returns Logs the error without rethrowing.
21
+ * @see {@link logException}
22
+ */
23
+ export function createExceptionLogger(
24
+ logger: DtLogger | undefined = undefined,
25
+ includeStack = false
26
+ ) {
27
+ let thatLogger: DtLogger;
28
+ if (logger) {
29
+ thatLogger = logger;
30
+ } else {
31
+ thatLogger = new DtLogger();
32
+ }
33
+
34
+ return (error: unknown) => {
35
+ logException(thatLogger, error, includeStack);
36
+ };
37
+ }
13
38
 
14
39
  /**
15
40
  * Log given exception with level ERROR to given logger.
16
41
  *
17
42
  * Supports AxiosError, Error and string
18
43
  *
19
- * @param logger DtLogger to use
20
- * @param error AxiosError, Error or string to log
21
- * @param includeStack Include stack in the message, default false
22
- * @see log
44
+ * @param logger - DtLogger to use
45
+ * @param error - AxiosError, Error or string to log
46
+ * @param [includeStack=true] - Include stack in the message, default false
47
+ * @returns Logs the error without rethrowing
48
+ * @see {@link DtLogger.log}
49
+ * @see {@link createExceptionLogger} for a curried setup
23
50
  */
24
51
  export function logException(
25
52
  logger: DtLogger,
26
- error: Error | string | AxiosError,
53
+ error: unknown,
27
54
  includeStack = false
28
55
  ) {
29
- const message = error instanceof Error ? error.message : error;
56
+ const message =
57
+ error instanceof Error
58
+ ? error.message
59
+ : typeof error === "string"
60
+ ? error
61
+ : JSON.stringify(error);
62
+
30
63
  const stack =
31
64
  error instanceof Error && includeStack ? error.stack : undefined;
32
- const code = error instanceof AxiosError ? error.code : undefined;
65
+
66
+ const customCode = error instanceof AxiosError ? error.code : undefined;
33
67
 
34
68
  logger.error({
35
69
  type: "Error",
36
70
  method: `${functionName}.logException`,
37
71
  message,
38
- code,
72
+ customCode,
39
73
  stack,
40
74
  });
41
75
  }