Skip to content

Commit 2c483d4

Browse files
committed
fix: improve error handling
1 parent a496324 commit 2c483d4

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

package-e2e/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ assertType<DetoxAllure2AdapterDeviceLogsOptions>({
3232

3333
assertType<DetoxAllure2AdapterDeviceScreenshotOptions>({
3434
saveAll: true,
35-
});
35+
});

src/listener.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const listener: EnvironmentListenerFn = (
2222
useSteps = false,
2323
deviceLogs = false,
2424
deviceScreenshots = false,
25+
onError,
2526
}: DetoxAllure2AdapterOptions = {},
2627
) => {
2728
let logHandler: ReturnType<typeof createLogHandler>;
@@ -47,13 +48,15 @@ export const listener: EnvironmentListenerFn = (
4748
logs = new LogBuffer({
4849
device: detox.device,
4950
options: deviceLogs,
51+
onError,
5052
});
5153
}
5254

5355
if (deviceScreenshots) {
5456
screenshots = new ScreenshotHelper({
5557
device: detox.device,
5658
options: deviceScreenshots,
59+
onError,
5760
});
5861
}
5962
})

src/logs/log-buffer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type AnyEntry = AndroidEntry & IosEntry;
1010
export interface LogBufferOptions {
1111
device: Detox.Device;
1212
options: true | DetoxAllure2AdapterDeviceLogsOptions;
13+
onError?: (error: Error) => void;
1314
}
1415

1516
export interface StepLogRecorder {
@@ -22,6 +23,8 @@ export interface StepLogRecorder {
2223
close(): Promise<void>;
2324
}
2425

26+
const noop = () => {};
27+
2528
export class LogBuffer implements StepLogRecorder {
2629
private readonly _emitter: Emitter;
2730
private _entries: AnyEntry[] = [];
@@ -48,6 +51,7 @@ export class LogBuffer implements StepLogRecorder {
4851
});
4952

5053
this._emitter.on('entry', this._onEntry);
54+
this._emitter.on('error', this._config.onError ?? noop);
5155
}
5256

5357
public resetPid() {
@@ -91,6 +95,7 @@ export class LogBuffer implements StepLogRecorder {
9195

9296
public async close() {
9397
await this._emitter.close();
98+
this._emitter.removeAllListeners();
9499
}
95100

96101
public attachBefore(allure: AllureRuntime) {

src/screenshots/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { DetoxAllure2AdapterDeviceScreenshotOptions } from '../types';
77
export interface ScreenshotHelperConfig {
88
device: Detox.Device;
99
options: true | DetoxAllure2AdapterDeviceScreenshotOptions;
10+
onError?: (error: Error) => void;
1011
}
1112

1213
export class ScreenshotHelper {
@@ -15,13 +16,13 @@ export class ScreenshotHelper {
1516
private readonly _options: DetoxAllure2AdapterDeviceScreenshotOptions;
1617
private readonly _kitten: Screenkitten;
1718

18-
constructor({ device, options }: ScreenshotHelperConfig) {
19+
constructor({ device, options, onError }: ScreenshotHelperConfig) {
1920
this._device = device;
2021
this._platform = device.getPlatform();
2122
this._options = typeof options === 'boolean' ? {} : options;
2223
this._kitten = screenkitten({
2324
platform: this._platform,
24-
onError: 'ignore', // Don't throw on errors, just log them
25+
onError: onError ?? 'ignore',
2526
});
2627
}
2728

src/steps/wrapWithSteps.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ export function wrapWithSteps(options: WrapWithStepsOptions) {
3737
const ws = worker._client._asyncWebSocket;
3838
const send = ws.send.bind(ws) as (...args: any[]) => Promise<{ type?: string }>;
3939
const onActionSuccess = async () => {
40-
await logs?.attachAfterSuccess(allure);
40+
logs?.attachAfterSuccess(allure);
4141
};
4242
const onActionFailure = async (shouldSetStatus: boolean) => {
4343
if (shouldSetStatus) {
4444
allure.status('failed');
4545
}
4646

4747
await screenshots?.attachFailure(allure);
48-
await logs?.attachAfterFailure(allure);
48+
logs?.attachAfterFailure(allure);
4949
};
5050
ws.send = async (...args: any[]) => {
5151
const desc = descriptionMaker(args[0]);
@@ -97,19 +97,20 @@ function wrapDeviceMethod(
9797
}
9898

9999
try {
100+
logs?.attachBefore(allure);
100101
const result = await originalMethod.apply(device, args);
101102

102103
if (PID_CHANGING_METHODS.has(methodName)) {
103104
logs?.refreshPid();
104105
}
105106

106107
await screenshots?.attach(allure, false);
107-
await logs?.attachAfterSuccess(allure);
108+
logs?.attachAfterSuccess(allure);
108109

109110
return result;
110111
} catch (error) {
111112
await screenshots?.attachFailure(allure);
112-
await logs?.attachAfterFailure(allure);
113+
logs?.attachAfterFailure(allure);
113114

114115
throw error; // Re-throw the error
115116
}

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export type DetoxAllure2AdapterOptions = {
1414
* Device screenshots configuration for per-step logging
1515
*/
1616
deviceScreenshots?: boolean | DetoxAllure2AdapterDeviceScreenshotOptions;
17+
/**
18+
* Callback to handle errors
19+
*/
20+
onError?: (error: Error) => void;
1721
};
1822

1923
export interface DetoxAllure2AdapterDeviceLogsOptions {

0 commit comments

Comments
 (0)