50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { Inject, Injectable } from "@nestjs/common";
|
|
import {
|
|
CLIENT_ERROR_THROTTLE_STORE,
|
|
type ClientErrorThrottleStore,
|
|
} from "./client-error-throttle.store.js";
|
|
|
|
const LOG_STACK_LIMIT = 4000;
|
|
|
|
export interface ClientErrorReport {
|
|
subject: string;
|
|
brandId: string;
|
|
errorId: string;
|
|
message: string;
|
|
route: string;
|
|
componentStack?: string | undefined;
|
|
requestId: string | null;
|
|
userAgent: string | null;
|
|
}
|
|
|
|
export interface ClientErrorReceipt {
|
|
received: true;
|
|
duplicate: boolean;
|
|
}
|
|
|
|
@Injectable()
|
|
export class AdminClientErrorService {
|
|
constructor(
|
|
@Inject(CLIENT_ERROR_THROTTLE_STORE)
|
|
private readonly throttle: ClientErrorThrottleStore,
|
|
) {}
|
|
|
|
async record(report: ClientErrorReport): Promise<ClientErrorReceipt> {
|
|
const result = await this.throttle.record(report);
|
|
if (result.duplicate) return { received: true, duplicate: true };
|
|
|
|
console.error("admin client error", {
|
|
errorId: report.errorId,
|
|
message: report.message,
|
|
route: report.route,
|
|
subject: report.subject,
|
|
brandId: report.brandId,
|
|
requestId: report.requestId,
|
|
userAgent: report.userAgent,
|
|
componentStack: report.componentStack?.slice(0, LOG_STACK_LIMIT) ?? null,
|
|
});
|
|
|
|
return { received: true, duplicate: false };
|
|
}
|
|
}
|