-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1075 from JckXia/handle-error-thrown
Add logic to handle graceful error handling when primitive errors are thrown by JS functions
- Loading branch information
Showing
7 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <napi.h> | ||
|
||
namespace { | ||
void Test(const Napi::CallbackInfo& info) { | ||
info[0].As<Napi::Function>().Call({}); | ||
} | ||
|
||
} // namespace | ||
Napi::Object InitErrorHandlingPrim(Napi::Env env) { | ||
Napi::Object exports = Napi::Object::New(env); | ||
exports.Set("errorHandlingPrim", Napi::Function::New<Test>(env)); | ||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = require('./common').runTest((binding) => { | ||
test(binding.errorHandlingPrim); | ||
}); | ||
|
||
function canThrow (binding, errorMessage, errorType) { | ||
try { | ||
binding.errorHandlingPrim(() => { | ||
throw errorMessage; | ||
}); | ||
} catch (e) { | ||
// eslint-disable-next-line valid-typeof | ||
assert(typeof e === errorType); | ||
assert(e === errorMessage); | ||
} | ||
} | ||
|
||
function test (binding) { | ||
canThrow(binding, '404 server not found!', 'string'); | ||
canThrow(binding, 42, 'number'); | ||
canThrow(binding, Symbol.for('newSym'), 'symbol'); | ||
canThrow(binding, false, 'boolean'); | ||
canThrow(binding, BigInt(123), 'bigint'); | ||
canThrow(binding, () => { console.log('Logger shutdown incorrectly'); }, 'function'); | ||
canThrow(binding, { status: 403, errorMsg: 'Not authenticated' }, 'object'); | ||
} |