Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/runtime/src/execute/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ export default (
}
}

// TODO naming
class FancyPromise extends Promise {
constructor(resolve, reject) {
super(resolve, reject);
}
reject(e: any) {
console.log(' >>> custom reject!!');
if (!e || !(e instanceof Error)) {
// Force all promise objects to be errors
// This allows us to track positions
const error = new Error();
Object.assign(error, e);
return super.reject(error);
}
return super.reject(e);
}
}

const context = vm.createContext(
freezeAll(
{
Expand All @@ -48,6 +66,7 @@ export default (
setTimeout,
state, // TODO will be dropped as a global one day, see https://github.com/OpenFn/kit/issues/17
Buffer: SafeBuffer,
Promise: FancyPromise,
},
{ state: true }
),
Expand Down
22 changes: 22 additions & 0 deletions packages/runtime/test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,28 @@ test('fail on runtime TypeError', async (t) => {
});
});

test.only('fail with position on rejected promise', async (t) => {
const expression = `export default [(s) => {
return Promise.reject({});
}]`;

const result: any = await run(expression);
const error = result.errors['job-1'];
t.log(error);

// t.deepEqual(error, {
// message: "TypeError: Cannot read properties of undefined (reading 'y')",
// name: 'RuntimeError',
// subtype: 'TypeError',
// severity: 'fail',
// source: 'runtime',
// pos: {
// column: 28,
// line: 1,
// },
// });
});

test('fail on runtime error with RangeError', async (t) => {
const expression =
'export default [(s) => Number.parseFloat("1").toFixed(-1)]';
Expand Down