Skip to content

optional way of automatic native exception wrapping #31

@farfromrefug

Description

@farfromrefug

Right now in the runtimes when a native exception is thrown which you catch with a try/catch it is the native exception object that you get.
So in my apps/N fork i use something like this:

export function wrapNativeException<T = any>(ex: NSError, wrapError: (...args) => T = (msg) => new Error(msg) as any) {
	if (!ex) {
		return;
	}
	if (typeof ex === 'string') {
		return wrapError(ex);
	}
	if (!(ex instanceof Error)) {
		const err = wrapError(ex.toString());
		err['nativeException'] = ex;
		//@ts-ignore
		err['stackTrace'] = com.tns.NativeScriptException.getStackTraceAsString(ex);
		return err;
	}
	return ex;
}

That method wraps the native exception in a JS error with stackTrace.

Would be pretty awesome to have that done automatically so that the error object you get in catch is an actual JS Error.
I think for now if we can do it it should be optional and disabled by default

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions