Skip to content

Improve error handling #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 9 additions & 9 deletions generator/templates/lib.tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ fn init(mut exports: JsObject, env: Env) -> Result<()> {
// do not uncomment until it is clean as it swallow the error message and
// makes things event more complicated...
//
// std::panic::set_hook(Box::new(|panic_info| {
// println!("{:?}", panic_info.payload());

// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
// println!("panic occurred: {s:?}");
// } else {
// println!("panic occurred");
// }
// }));
std::panic::set_hook(Box::new(|panic_info| {
// println!("> panic catched: {:?}", panic_info.payload());

// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
// println!("panic occurred: {s:?}");
// } else {
// println!("panic occurred");
// }
}));


// Store constructor for factory methods and internal instantiations
Expand Down
6 changes: 4 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const nativeModule = require('./index.cjs');
console.log(nativeModule);
export const {
AudioContext,
OfflineAudioContext,
Expand All @@ -47,11 +48,12 @@ export const {
PannerNode,
StereoPannerNode,
WaveShaperNode,

// helper methods
mediaDevices,
// for wpt tests (maybe not required actually)
NotSupportedError
} = nativeModule;

export default nativeModule;



39 changes: 39 additions & 0 deletions monkey-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,48 @@ function patchOfflineAudioContext(nativeBinding) {
return OfflineAudioContext;
}

function monkeyPatchGainNode(nativeBinding) {
class GainNode extends nativeBinding.GainNode {
constructor(...args) {
super(...args);

const ctorName = this.constructor.name;

return new Proxy(this, {
get(obj, prop) {

},
set(obj, prop, value) {
try {
return Reflect.set(obj, prop, value);
} catch (err) {
if (err.message.startsWith('NotSupportedError')) {
let msg = `Failed to set the "${prop}" property on "${ctorName}":`
msg += ` ${err.message.replace(/^NotSupportedError - /, '')}`

throw new NotSupportedError(msg);
}
}

return true;
},
apply(target, thisArg, argumentsList) {

},
});
}
}

return GainNode;
}

module.exports = function monkeyPatch(nativeBinding) {
nativeBinding.AudioContext = patchAudioContext(nativeBinding);
nativeBinding.OfflineAudioContext = patchOfflineAudioContext(nativeBinding);
nativeBinding.GainNode = monkeyPatchGainNode(nativeBinding);

// for wpt tests
nativeBinding.NotSupportedError = NotSupportedError;

// Promisify MediaDevices API
enumerateDevicesSync = nativeBinding.mediaDevices.enumerateDevices;
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ fn init(mut exports: JsObject, env: Env) -> Result<()> {
// do not uncomment until it is clean as it swallow the error message and
// makes things event more complicated...
//
// std::panic::set_hook(Box::new(|panic_info| {
// println!("{:?}", panic_info.payload());

// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
// println!("panic occurred: {s:?}");
// } else {
// println!("panic occurred");
// }
// }));
std::panic::set_hook(Box::new(|panic_info| {
// println!("> panic catched: {:?}", panic_info.payload());

// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
// println!("panic occurred: {s:?}");
// } else {
// println!("panic occurred");
// }
}));

// Store constructor for factory methods and internal instantiations
// Note that we need to create the js class twice so that export and store
Expand Down