Skip to content

Commit

Permalink
fix: make HTTP replies more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 15, 2020
1 parent 73b9cfd commit 4b98e64
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/cosmic-swingset/lib/ag-solo/vats/vat-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ export function buildRootObject(vatPowers) {
const channelID = channelHandleToId.get(channelHandle);
if (channelID) {
const o = { ...obj, meta: { channelID } };
D(commandDevice).sendBroadcast(o);
D(commandDevice).sendBroadcast(JSON.parse(JSON.stringify(o)));
}
}
};

const sendResponse = (count, isException, obj) =>
D(commandDevice).sendResponse(
count,
isException,
obj || JSON.parse(JSON.stringify(obj)),
);

const registeredURLHandlers = new Map();

async function registerURLHandler(handler, url) {
Expand Down Expand Up @@ -182,25 +189,21 @@ export function buildRootObject(vatPowers) {
const res = await E(urlHandlers[i])[dispatcher](obj, meta);

if (res) {
D(commandDevice).sendResponse(count, false, harden(res));
sendResponse(count, false, res);
return;
}
}
}

if (dispatcher === 'onMessage') {
D(commandDevice).sendResponse(
count,
false,
harden({ type: 'doesNotUnderstand', obj }),
);
sendResponse(count, false, { type: 'doesNotUnderstand', obj });
throw Error(`No handler for ${url} ${type}`);
}
D(commandDevice).sendResponse(count, false, harden(true));
sendResponse(count, false, true);
} catch (rej) {
console.debug(`Error ${dispatcher}:`, rej);
const jsonable = (rej && rej.message) || rej;
D(commandDevice).sendResponse(count, true, harden(jsonable));
sendResponse(count, true, jsonable);
}
},
});
Expand Down

0 comments on commit 4b98e64

Please sign in to comment.