Skip to content

Commit 08c88c9

Browse files
committedOct 23, 2024
fix(ios): remove interop.NSErrorWrapper
1 parent 37ffb12 commit 08c88c9

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed
 

‎src/audio/player.ios.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Observable, Utils, knownFolders, path as nsFilePath } from '@nativescript/core';
22
import { AudioPlayerOptions } from '.';
3+
import { wrapNativeException } from './utils.ios';
34

45
declare let AVAudioPlayer;
56

@@ -144,7 +145,7 @@ export class TNSPlayer extends Observable {
144145
const errorRef = new interop.Reference<NSError>();
145146
this._player = AVAudioPlayer.alloc().initWithContentsOfURLError(NSURL.fileURLWithPath(fileName), errorRef);
146147
if (errorRef && errorRef.value) {
147-
throw new interop.NSErrorWrapper(errorRef.value);
148+
throw wrapNativeException(errorRef.value);
148149
} else if (this._player) {
149150
this.handleStartPlayer(options);
150151

@@ -183,7 +184,7 @@ export class TNSPlayer extends Observable {
183184
const errorRef = new interop.Reference<NSError>();
184185
this._player = AVAudioPlayer.alloc().initWithDataError(data, errorRef);
185186
if (errorRef && errorRef.value) {
186-
throw new interop.NSErrorWrapper(errorRef.value);
187+
throw wrapNativeException(errorRef.value);
187188
} else if (this._player) {
188189
this.handleStartPlayer(options);
189190

‎src/audio/recorder.ios.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Observable } from '@nativescript/core';
22
import { AudioRecorderOptions } from '.';
3+
import { wrapNativeException } from './utils.ios';
34

45
@NativeClass()
56
class TNSRecorderDelegate extends NSObject implements AVAudioRecorderDelegate {
@@ -84,7 +85,7 @@ export class TNSRecorder extends Observable {
8485
);
8586
//@ts-ignore
8687
if (errorRef && errorRef.value) {
87-
throw new interop.NSErrorWrapper(errorRef.value);
88+
throw wrapNativeException(errorRef.value);
8889
}
8990

9091
this._recordingSession.setActiveError(true);
@@ -131,7 +132,7 @@ export class TNSRecorder extends Observable {
131132
this._recorder = AVAudioRecorder.alloc().initWithURLSettingsError(url, recordSetting, errorRef);
132133
}
133134
if (errorRef && errorRef.value) {
134-
throw new interop.NSErrorWrapper(errorRef.value);
135+
throw wrapNativeException(errorRef.value);
135136
} else {
136137
if (!this._recorder.delegate) {
137138
this._recorder.delegate = TNSRecorderDelegate.initWithOwner(this);

‎src/audio/utils.ios.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function wrapNativeException<T = any>(ex: NSError, wrapError: (...args) => T = (msg) => new Error(msg) as any) {
2+
if (!ex) {
3+
return null;
4+
}
5+
if (typeof ex === 'string') {
6+
return wrapError(ex);
7+
}
8+
if (!(ex instanceof Error)) {
9+
const err = wrapError(ex.localizedDescription);
10+
err['nativeException'] = ex;
11+
err['code'] = ex.code;
12+
err['domain'] = ex.domain;
13+
// TODO: we loose native stack. see how to get it
14+
return err;
15+
}
16+
return ex;
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.