@flanksource/clicky-ui
Version:
Flanksource Clicky UI — React component library built on shadcn/ui with light/dark and density theming.
109 lines (108 loc) • 3.73 kB
JavaScript
function normalizeErrorDiagnostics(value, fallback) {
if (!value) return null;
if (typeof value === "string") {
return value.trim() ? { message: value, context: [] } : null;
}
const record = objectRecord(value);
if (!record) return null;
const nested = objectRecord(record.error) ?? objectRecord(record.diagnostics);
if (nested && nested !== record) {
return normalizeErrorDiagnostics(nested, fallback);
}
const message = firstString(record, ["error", "message", "msg", "reason", "detail", "details"]) ?? fallback;
const trace = firstString(record, ["trace", "trace_id", "traceId", "traceID"]);
const stacktrace = firstString(record, ["stacktrace", "stack_trace", "stackTrace", "stack"]);
const time = firstString(record, ["time", "timestamp", "created_at"]);
const context = contextEntries(record.context);
if (!message && !trace && !stacktrace && context.length === 0) return null;
return {
message: message ?? "Action failed",
context,
raw: value,
...trace !== void 0 ? { trace } : {},
...time !== void 0 ? { time } : {},
...stacktrace !== void 0 ? { stacktrace } : {}
};
}
function parseDiagnosticsStackTrace(stacktrace) {
const lines = stacktrace.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
const frames = [];
const unparsed = [];
let headline;
for (const line of lines) {
const frame = parseStackTraceFrame(line);
if (frame) {
frames.push(frame);
continue;
}
if (!headline && !line.startsWith("--- at ")) {
headline = line;
continue;
}
unparsed.push(line);
}
return {
frames,
unparsed,
raw: stacktrace,
...headline !== void 0 ? { headline } : {}
};
}
function parseInlineJsonContextValue(value) {
const trimmed = value.trim();
if (!trimmed || !/^[{[]/.test(trimmed)) return null;
try {
return JSON.parse(trimmed);
} catch {
return null;
}
}
function compactStackPath(file) {
return file.replace(/^github\.com\/flanksource\/incident-commander\//, "").replace(/^github\.com\/flanksource\//, "flanksource/").replace(/^.*\/go\/pkg\/mod\//, "pkg/mod/").replace(/^.*\/incident-commander\//, "");
}
function isApplicationStackFrame(file) {
return file.includes("github.com/flanksource/incident-commander/") || file.includes("/incident-commander/");
}
function parseStackTraceFrame(line) {
var _a;
const match = line.match(/^--- at (.+):(\d+)(?:\s+(.+))?$/);
if (!match) return null;
return {
raw: line,
file: match[1] ?? "",
line: Number(match[2] ?? 0),
...((_a = match[3]) == null ? void 0 : _a.trim()) ? { functionName: match[3].trim() } : {}
};
}
function objectRecord(value) {
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
return value;
}
function firstString(record, keys) {
for (const key of keys) {
const value = record[key];
if (typeof value === "string" && value.trim()) return value;
}
return void 0;
}
function contextEntries(value) {
const record = objectRecord(value);
if (!record) return [];
return Object.entries(record).filter(
([, entryValue]) => entryValue !== void 0 && entryValue !== null && entryValue !== ""
).map(([key, entryValue]) => [key, stringifyValue(entryValue)]);
}
function stringifyValue(value) {
if (value === null || value === void 0 || value === "") return "-";
if (typeof value === "string") return value;
if (typeof value === "number" || typeof value === "boolean") return String(value);
return JSON.stringify(value);
}
export {
compactStackPath,
isApplicationStackFrame,
normalizeErrorDiagnostics,
parseDiagnosticsStackTrace,
parseInlineJsonContextValue
};
//# sourceMappingURL=error-diagnostics.js.map