Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Sep 3, 2023
1 parent c59ef2b commit 7e49053
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/composition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnyAsync, AsyncFunction, Func, Last } from "./typing.ts";
import { reverse, Reversed } from "./array.ts";
import { last, reverse, Reversed } from "./array.ts";

import { not } from "./operator.ts";
import { reduce } from "./reduce.ts";
Expand Down Expand Up @@ -45,25 +45,26 @@ interface StackFrame {
const frameToString = ({ line, file, column }: StackFrame) =>
`${file}:${line}:${column}`;

const parseStackLine = (stackLine: string): null | StackFrame => {
const matches = RegExp(/\s+at\s+(.+\s)?\(?(.+):(\d+):(\d+)\)?/).exec(
stackLine,
);
if (!matches) return null;
const [, , file, line, column] = matches;
return { file, line: parseInt(line), column: parseInt(column) };
};

const parseStackTrace = (trace: string) =>
trace.split("\n")
.slice(1)
.map((stackLine: string): StackFrame => {
const matches = RegExp(/\s+at\s+(.+\s)?\(?(.+):(\d+):(\d+)\)?/).exec(
stackLine,
);
if (!matches) throw new Error(stackLine);
const [, , file, line, column] = matches;
return { file, line: parseInt(line), column: parseInt(column) };
});
parseStackLine(last(trace.split("\n")));

const errorBoundry = <F extends Func>(f: F) => {
const stack = parseStackTrace(new Error().stack as string).map(frameToString);
const err = new Error().stack as string;
return ((...x) => {
try {
return f(...x);
} catch (e) {
console.error(stack[stack.length - 1]);
const lastStackCall = parseStackTrace(err);
if (lastStackCall) console.error(frameToString(lastStackCall));
throw e;
}
}) as F;
Expand Down

0 comments on commit 7e49053

Please sign in to comment.