Skip to content

Commit 8066c95

Browse files
committed
Improved error handling for image loading
1 parent 27c1680 commit 8066c95

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

lib/adapter/vision.adapter.class.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ export class VisionAdapter {
5757
public async findOnScreenRegion(
5858
matchRequest: MatchRequest,
5959
): Promise<MatchResult> {
60-
const matchResult = await this.finder.findMatch(matchRequest);
61-
return Promise.resolve(matchResult);
60+
return new Promise<MatchResult>(async (resolve, reject) => {
61+
try {
62+
const matchResult = await this.finder.findMatch(matchRequest);
63+
resolve(matchResult);
64+
} catch (e) {
65+
reject(e);
66+
}
67+
});
6268
}
6369

6470
/**

lib/provider/opencv/template-matching-finder.class.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ describe("Template-matching finder", () => {
6262
// THEN
6363
await expect(result)
6464
.rejects
65-
.toEqual(`Failed to load ${pathToHaystack}. Reason: 'Failed to load image from '${pathToHaystack}''.`);
65+
.toThrowError(`Failed to load ${pathToHaystack}. Reason: 'Failed to load image from '${pathToHaystack}''.`);
6666
});
6767
});

lib/provider/opencv/template-matching-finder.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class TemplateMatchingFinder implements FinderInterface {
198198
}
199199
resolve(matches[0]);
200200
} catch (e) {
201-
reject(e.message);
201+
reject(e);
202202
}
203203
});
204204
}

lib/screen.class.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ describe("Screen.", () => {
6161
.toEqual(`No match for ${imagePath}. Required: ${SUT.config.confidence}, given: ${matchResult.confidence}`);
6262
});
6363

64+
it("should reject when search fails.", async () => {
65+
const rejectionReason = "Search failed.";
66+
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
67+
return Promise.reject(rejectionReason);
68+
});
69+
70+
const visionAdapterMock = new VisionAdapter();
71+
72+
const SUT = new Screen(visionAdapterMock);
73+
const imagePath = "test/path/to/image.png";
74+
await expect(SUT.find(imagePath))
75+
.rejects
76+
.toEqual(`Searching for ${imagePath} failed. Reason: '${rejectionReason}'`);
77+
});
78+
6479
it("should override default confidence value with parameter.", async () => {
6580
const minMatch = 0.8;
6681
const matchResult = new MatchResult(minMatch, searchRegion);

lib/screen.class.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,21 @@ export class Screen {
4444
minMatch,
4545
);
4646

47-
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
48-
49-
return new Promise<Region>((resolve, reject) => {
50-
if (matchResult.confidence >= minMatch) {
51-
resolve(matchResult.location);
52-
} else {
47+
return new Promise<Region>(async (resolve, reject) => {
48+
try {
49+
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
50+
if (matchResult.confidence >= minMatch) {
51+
resolve(matchResult.location);
52+
} else {
53+
reject(
54+
`No match for ${pathToNeedle}. Required: ${minMatch}, given: ${
55+
matchResult.confidence
56+
}`,
57+
);
58+
}
59+
} catch (e) {
5360
reject(
54-
`No match for ${pathToNeedle}. Required: ${minMatch}, given: ${
55-
matchResult.confidence
56-
}`,
61+
`Searching for ${pathToNeedle} failed. Reason: '${e}'`,
5762
);
5863
}
5964
});

0 commit comments

Comments
 (0)