錯誤處理
如果您在執行指令時遇到失敗,並且需要因此讓測試案例失敗,您必須回應 IDE 的錯誤。
由於 JSON 不支援錯誤序列化,因此 IDE 制定了一個標準。
錯誤
錯誤物件是一個正常的 JavaScript 物件,當傳送到 IDE 時,將會被解析為錯誤。
{
status: "fatal",
error: "This command can't be run individually, please run the test case."
}
狀態- 選擇性,可以是未定義或致命,致命錯誤將使測試失敗,非致命錯誤將繼續執行,對於驗證指令很有用。錯誤- 必填,要印給使用者的訊息。
傳送錯誤
在執行期間遇到錯誤時,可以使用 sendResponse 回覆錯誤物件。
browser.runtime.onMessageExternal.addListener((message, sender, sendResponse) => {
if (message.action === "execute" && message.command && message.command.command === "myFailingCommand") {
executingSomeFunctionalityThatWillEventuallyFail(message.command).catch((error) => {
return sendResponse({ error: error.message, status: "fatal" });
});
return true;
}
});
