Skip to content

Commit

Permalink
fix(logging): Resolved issues with creation of extra Pino transports
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Jan 1, 2024
1 parent d918cb3 commit cf88fc1
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 375 deletions.
10 changes: 7 additions & 3 deletions docs/api-reports/packages/errors/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ function createStormError<TCode extends string = string>({
stack,
data
}: StormErrorOptions & {
code: TCode;
code?: TCode;
}): StormError<TCode>;
export { createStormError };
export { createStormError as createStormError_alias_1 };
export { createStormError as createStormError_alias_2 };

// @public
function deserializeStormError(json: JsonValue): StormError;
Expand Down Expand Up @@ -70,6 +71,7 @@ export { ErrorCode as ErrorCode_alias_1 };
function getCauseFromUnknown(cause: unknown): StormError;
export { getCauseFromUnknown };
export { getCauseFromUnknown as getCauseFromUnknown_alias_1 };
export { getCauseFromUnknown as getCauseFromUnknown_alias_2 };

// @public
function isStormError<TCode extends string = any>(
Expand All @@ -93,14 +95,16 @@ class StormError<TCode extends string = string> extends Error {
);
// (undocumented)
__proto__: ErrorConstructor;
cause?: StormError;
get cause(): StormError | undefined;
set cause(_cause: unknown);
code: TCode;
static create(error?: unknown): StormError;
data?: any;
get originalStack(): string;
print(): string;
get stack(): string;
set stack(_stack: string);
toString(): string;
toString(stacktrace?: boolean): string;
}
export { StormError };
export { StormError as StormError_alias_1 };
Expand Down
38 changes: 27 additions & 11 deletions docs/api-reports/packages/logging/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,34 @@
import { DestinationStream } from "pino";
import { Logger } from "pino";
import { LoggerOptions as LoggerOptions_2 } from "pino";
import { SerializerFn } from "pino";
import { StormConfig } from "@storm-software/config-tools";
import { Temporal } from "@js-temporal/polyfill";
import * as z from "zod";

// @public (undocumented)
export const createErrorSerializer: SerializerFn;

// @public
export const getLogLevel: (label: string) => LogLevel;
const getLogLevel: (label: string) => LogLevel;
export { getLogLevel };
export { getLogLevel as getLogLevel_alias_1 };
export { getLogLevel as getLogLevel_alias_2 };

// @public
export const getLogLevelLabel: (logLevel: number) => LogLevelLabel;
const getLogLevelLabel: (logLevel: number) => LogLevelLabel;
export { getLogLevelLabel };
export { getLogLevelLabel as getLogLevelLabel_alias_1 };
export { getLogLevelLabel as getLogLevelLabel_alias_2 };

// @public
const getTransports: (
const getPinoLogger: (
config: StormConfig<"logging", LoggingConfig>,
name?: string
) => Logger<any>;
export { getTransports };
export { getTransports as getTransports_alias_1 };
export { getTransports as getTransports_alias_2 };
) => Logger<LoggerOptions_2>;
export { getPinoLogger };
export { getPinoLogger as getPinoLogger_alias_1 };
export { getPinoLogger as getPinoLogger_alias_2 };

// @public (undocumented)
interface ILogger {
Expand Down Expand Up @@ -166,10 +176,10 @@ export const LoggingConfigSchema: z.ZodObject<
>;

// @public (undocumented)
export type LogLevel = 0 | 10 | 20 | 30 | 40 | 60 | 70;
type LogLevel = 0 | 10 | 20 | 30 | 40 | 60 | 70;

// @public (undocumented)
export const LogLevel: {
const LogLevel: {
SILENT: LogLevel;
FATAL: LogLevel;
ERROR: LogLevel;
Expand All @@ -178,9 +188,12 @@ export const LogLevel: {
DEBUG: LogLevel;
TRACE: LogLevel;
};
export { LogLevel };
export { LogLevel as LogLevel_alias_1 };
export { LogLevel as LogLevel_alias_2 };

// @public (undocumented)
export type LogLevelLabel =
type LogLevelLabel =
| "silent"
| "fatal"
| "error"
Expand All @@ -190,7 +203,7 @@ export type LogLevelLabel =
| "trace";

// @public (undocumented)
export const LogLevelLabel: {
const LogLevelLabel: {
SILENT: LogLevelLabel;
FATAL: LogLevelLabel;
ERROR: LogLevelLabel;
Expand All @@ -199,6 +212,9 @@ export const LogLevelLabel: {
DEBUG: LogLevelLabel;
TRACE: LogLevelLabel;
};
export { LogLevelLabel };
export { LogLevelLabel as LogLevelLabel_alias_1 };
export { LogLevelLabel as LogLevelLabel_alias_2 };

// @public
export const LokiConfigSchema: z.ZodObject<
Expand Down
47 changes: 40 additions & 7 deletions docs/api-reports/packages/logging/documents-model.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@
"name": "",
"preserveMemberOrder": false,
"members": [
{
"kind": "Variable",
"canonicalReference": "@storm-stack/logging!createErrorSerializer:var",
"docComment": "",
"excerptTokens": [
{
"kind": "Content",
"text": "createErrorSerializer: "
},
{
"kind": "Reference",
"text": "SerializerFn",
"canonicalReference": "!unknown"
}
],
"fileUrlPath": "packages/logging/src/utilities/log-serializer.ts",
"isReadonly": true,
"releaseTag": "Public",
"name": "createErrorSerializer",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
}
},
{
"kind": "Variable",
"canonicalReference": "@storm-stack/logging!getLogLevel:var",
Expand Down Expand Up @@ -230,12 +254,12 @@
},
{
"kind": "Variable",
"canonicalReference": "@storm-stack/logging!getTransports:var",
"canonicalReference": "@storm-stack/logging!getPinoLogger:var",
"docComment": "/**\n * Get the Pino transports for the logger\n *\n * @param config - The workspace config\n *\n * @param name - The name of the logger to get the options for\n *\n * @returns The options for the logger\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "getTransports: "
"text": "getPinoLogger: "
},
{
"kind": "Content",
Expand All @@ -257,16 +281,25 @@
},
{
"kind": "Content",
"text": ">, name?: string) => import(\"pino\").Logger<any>"
"text": ">, name?: string) => import(\"pino\").Logger<"
},
{
"kind": "Reference",
"text": "PinoLoggerOptions",
"canonicalReference": "!unknown"
},
{
"kind": "Content",
"text": ">"
}
],
"fileUrlPath": "packages/logging/src/utilities/get-transports.ts",
"fileUrlPath": "packages/logging/src/utilities/pino-logger.ts",
"isReadonly": true,
"releaseTag": "Public",
"name": "getTransports",
"name": "getPinoLogger",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 6
"endIndex": 8
}
},
{
Expand Down Expand Up @@ -1358,7 +1391,7 @@
"text": ";"
}
],
"fileUrlPath": "packages/logging/src/utilities/get-transports.ts",
"fileUrlPath": "packages/logging/src/utilities/pino-logger.ts",
"releaseTag": "Public",
"name": "LoggerOptions",
"typeTokenRange": {
Expand Down
126 changes: 37 additions & 89 deletions packages/errors/src/storm-error.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Serializable } from "@storm-stack/serialization";
import {
EMPTY_STRING,
Indexable,
NEWLINE_STRING,
isObject
} from "@storm-stack/utilities";
import { EMPTY_STRING, NEWLINE_STRING } from "@storm-stack/utilities";
import StackTracey from "stacktracey";
import { ErrorCode } from "./errors";
import { isStormError } from "./utilities";
import { getCauseFromUnknown, isStormError } from "./utilities";
import {
deserializeStormError,
serializeStormError
Expand All @@ -22,81 +16,6 @@ export interface StormErrorOptions {
data?: any;
}

/**
* Creates a new StormError instance
*
* @param cause - The cause of the error
* @returns The newly created StormError
*/
export function createStormError<TCode extends string = string>({
code,
name,
message,
cause,
stack,
data
}: StormErrorOptions & { code: TCode }): StormError<TCode> {
if (isStormError(cause)) {
return cause;
}

if (cause instanceof Error && cause.name === "StormError") {
return cause as StormError<TCode>;
}

const stormError = new StormError<TCode>(code, {
name,
message,
cause,
stack,
data
});

// Inherit stack from error
if (cause instanceof Error && cause.stack) {
stormError.stack = cause.stack;
}

return stormError;
}

/**
* Gets the cause of an unknown error and returns it as a StormError
*
* @param cause - The cause of the error in an unknown type
* @returns The cause of the error in a StormError object or undefined
*/
export function getCauseFromUnknown(cause: unknown): StormError {
if (isStormError(cause)) {
return cause;
}

const type = typeof cause;
if (type === "undefined" || type === "function" || cause === null) {
return new StormError(ErrorCode.internal_server_error, {
cause
});
}

// Primitive types just get wrapped in an error
if (type !== "object") {
return new StormError(ErrorCode.internal_server_error, {
message: String(cause)
});
}

// If it's an object, we'll create a synthetic error
if (isObject(cause)) {
const err = new StormError(ErrorCode.unknown_cause, {});
for (const key in cause) {
(err as Indexable)[key] = (cause as Indexable)[key];
}
return err;
}

return new StormError(ErrorCode.internal_server_error, { cause });
}

/**
* A wrapper around the base JavaScript Error class to be used by Storm Software
*
Expand All @@ -115,20 +34,30 @@ export class StormError<TCode extends string = string> extends Error {
#stack?: string;

/**
* The error code
* The inner error
*/
public code!: TCode;
#cause?: StormError;

/**
* The cause of the error
* The error code
*/
public override cause?: StormError;
public code!: TCode;

/**
* Additional data to be passed with the error
*/
public data?: any;

/**
* Creates a new StormError instance
*
* @param error - The error to create
* @returns The newly created StormError
*/
public static create(error?: unknown): StormError {
return getCauseFromUnknown(error);
}

public constructor(
code: TCode,
{ name, message, cause, stack, data }: StormErrorOptions = {
Expand All @@ -152,6 +81,20 @@ export class StormError<TCode extends string = string> extends Error {
Object.setPrototypeOf(this, StormError.prototype);
}

/**
* The cause of the error
*/
public override get cause(): StormError | undefined {
return this.#cause;
}

/**
* The cause of the error
*/
public override set cause(_cause: unknown) {
this.#cause = getCauseFromUnknown(_cause);
}

/**
* Prints a displayable, formatted the stack trace
*
Expand Down Expand Up @@ -210,7 +153,12 @@ export class StormError<TCode extends string = string> extends Error {
*
* @returns The error message and stack trace string
*/
public override toString(): string {
return `${this.print} ${NEWLINE_STRING}Stack Trace: ${NEWLINE_STRING}${this.stack}`;
public override toString(stacktrace?: boolean): string {
return (
this.print() +
(stacktrace !== false
? ` ${NEWLINE_STRING}Stack Trace: ${NEWLINE_STRING}${this.stack}`
: "")
);
}
}

0 comments on commit cf88fc1

Please sign in to comment.