Skip to content

Commit 6018d0a

Browse files
author
farfromrefug
committed
fix(image): better error handling
1 parent a1f4614 commit 6018d0a

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

src/image/index-common.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,30 @@ export interface ImageError {
103103
toString(): string;
104104
}
105105

106+
export function wrapNativeException(ex, errorType = typeof ex) {
107+
if (typeof ex === 'string') {
108+
return new Error(ex);
109+
}
110+
if (!(ex instanceof Error)) {
111+
if (__ANDROID__) {
112+
const err = new Error(ex.toString());
113+
err['nativeException'] = ex;
114+
//@ts-ignore
115+
err['stackTrace'] = com.tns.NativeScriptException.getStackTraceAsString(ex);
116+
return err;
117+
}
118+
if (__IOS__) {
119+
const err = new Error(ex.localizedDescription);
120+
err['nativeException'] = ex;
121+
err['code'] = ex.code;
122+
err['domain'] = ex.domain;
123+
// TODO: we loose native stack. see how to get it
124+
return err;
125+
}
126+
}
127+
return ex;
128+
}
129+
106130
export interface ImagePipelineConfigSetting {
107131
isDownsampleEnabled?: boolean;
108132
leakTracker?: any;

src/image/index.android.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ import {
3232
showProgressBarProperty,
3333
srcProperty,
3434
stretchProperty,
35-
tintColorProperty
35+
tintColorProperty,
36+
wrapNativeException
3637
} from './index-common';
38+
import { FailureEventData } from '@nativescript-community/ui-image';
3739

3840
let initialized = false;
3941
let initializeConfig: ImagePipelineConfigSetting;
@@ -340,18 +342,6 @@ export class IntermediateEventData extends EventData {
340342
}
341343
}
342344

343-
export class FailureEventData extends EventData {
344-
private _error: ImageError;
345-
346-
get error(): ImageError {
347-
return this._error;
348-
}
349-
350-
set error(value: ImageError) {
351-
this._error = value;
352-
}
353-
}
354-
355345
export const needUpdateHierarchy = function (targetOrNeedsLayout: any, propertyKey?: string | Symbol, descriptor?: PropertyDescriptor): any {
356346
if (typeof targetOrNeedsLayout === 'boolean') {
357347
return function (target2: any, propertyKey: string | Symbol, descriptor: PropertyDescriptor) {
@@ -668,7 +658,7 @@ export class Img extends ImageBase {
668658
const imageError = new ImageError(throwable);
669659
nativeView.notify({
670660
eventName,
671-
error: imageError
661+
error: wrapNativeException(throwable)
672662
} as FailureEventData);
673663
}
674664
}
@@ -681,10 +671,9 @@ export class Img extends ImageBase {
681671
if (nativeView) {
682672
const eventName = ImageBase.intermediateImageFailedEvent;
683673
if (nativeView.hasListeners(eventName)) {
684-
const imageError = new ImageError(throwable);
685674
nativeView.notify({
686675
eventName,
687-
error: imageError
676+
error: wrapNativeException(throwable)
688677
} as FailureEventData);
689678
}
690679
}

src/image/index.d.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,21 +314,11 @@ export class IntermediateEventData {
314314
/**
315315
* Instances of this class are provided to the handlers of the {@link failure} and {@link intermediateImageFailed}.
316316
*/
317-
export class FailureEventData {
318-
/**
319-
* Returns the name of the event that has been fired.
320-
*/
321-
eventName: string;
322-
323-
/**
324-
* The object that fires the event.
325-
*/
326-
object: any;
327-
317+
export class FailureEventData extends EventData {
328318
/**
329319
* An object containing information about the status of the event.
330320
*/
331-
error: ImageError;
321+
error: Error;
332322
}
333323

334324
/**

src/image/index.ios.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import {
1616
imageRotationProperty,
1717
placeholderImageUriProperty,
1818
srcProperty,
19-
stretchProperty
19+
stretchProperty,
20+
wrapNativeException
2021
} from './index-common';
22+
import { FailureEventData } from '@nativescript-community/ui-image';
2123

2224
export class ImageInfo implements ImageInfoBase {
2325
constructor(
@@ -402,8 +404,8 @@ export class Img extends ImageBase {
402404
if (error) {
403405
this.notify({
404406
eventName: Img.failureEvent,
405-
error
406-
});
407+
error: wrapNativeException(error)
408+
} as FailureEventData);
407409
if (this.failureImageUri) {
408410
image = this.getUIImage(this.failureImageUri);
409411
this._setNativeImage(image, animate);

0 commit comments

Comments
 (0)