Skip to content

Commit

Permalink
fix: move onAborted handler at top
Browse files Browse the repository at this point in the history
  • Loading branch information
bbtgnn committed Dec 12, 2023
1 parent f6f5673 commit a00f9e1
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,22 @@ const generateRoutes = (app: TemplatedApp) => {
const s = new Slangroom([http, wallet]);

app.post(path, (res, req) => {
/**
* Code may break on `slangroom.execute`
* so it's important to attach the `onAborted` handler before everything else
*/
res.onAborted(() => {
res.writeStatus('500').writeHeader('Content-Type', 'application/json').end('Aborted');
});

res.cork(() => {
try {
res.onData(async (d) => {
try {
const data = handleArrayBuffer(d);
validateData(schema, data);

res.onAborted(() => {
res
.writeStatus('500')
.writeHeader('Content-Type', 'application/json')
.end('Aborted');
});

const { result, logs } = await s.execute(contract, { keys, data, conf });
const { result } = await s.execute(contract, { keys, data, conf });

res
.writeStatus('200 OK')
Expand All @@ -146,6 +147,14 @@ const generateRoutes = (app: TemplatedApp) => {
});

app.get(path, async (res, req) => {
/**
* Code may break on `slangroom.execute`
* so it's important to attach the `onAborted` handler before everything else
*/
res.onAborted(() => {
res.writeStatus('500').writeHeader('Content-Type', 'application/json').end('Aborted');
});

res.cork(async () => {
try {
const data: Record<string, unknown> = {};
Expand All @@ -158,11 +167,8 @@ const generateRoutes = (app: TemplatedApp) => {
}
validateData(schema, data);

res.onAborted(() => {
res.writeStatus('500').writeHeader('Content-Type', 'application/json').end('Aborted');
});
const { result } = await s.execute(contract, { keys, conf, data });

const { result, logs } = await s.execute(contract, { keys, conf, data });
res
.writeStatus('200 OK')
.writeHeader('Content-Type', 'application/json')
Expand Down

0 comments on commit a00f9e1

Please sign in to comment.