Skip to content

Commit 27c1680

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

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ describe("Template-matching finder", () => {
6060
const result = SUT.findMatch(matchRequest);
6161

6262
// THEN
63-
expect(result).rejects.toEqual(`Failed to load image from '${pathToHaystack}'`);
63+
await expect(result)
64+
.rejects
65+
.toEqual(`Failed to load ${pathToHaystack}. Reason: 'Failed to load image from '${pathToHaystack}''.`);
6466
});
6567
});

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class TemplateMatchingFinder implements FinderInterface {
124124
needle = await this.loadNeedle(needleInput);
125125
} catch (e) {
126126
throw new Error(
127-
`Failed to load ${matchRequest.pathToNeedle}. Reason: '${e.message}'.`,
127+
`Failed to load ${matchRequest.pathToNeedle}. Reason: '${e}'.`,
128128
);
129129
}
130130
if (!needle || needle.empty) {
@@ -190,12 +190,16 @@ export class TemplateMatchingFinder implements FinderInterface {
190190
}
191191

192192
public async findMatch(matchRequest: MatchRequest, debug: boolean = false): Promise<MatchResult> {
193-
const matches = await this.findMatches(matchRequest, debug);
194-
return new Promise<MatchResult>((resolve, reject) => {
195-
if (matches.length === 0) {
196-
reject(`Unable to locate ${matchRequest.pathToNeedle}, no match!`);
193+
return new Promise<MatchResult>(async (resolve, reject) => {
194+
try {
195+
const matches = await this.findMatches(matchRequest, debug);
196+
if (matches.length === 0) {
197+
reject(`Unable to locate ${matchRequest.pathToNeedle}, no match!`);
198+
}
199+
resolve(matches[0]);
200+
} catch (e) {
201+
reject(e.message);
197202
}
198-
resolve(matches[0]);
199203
});
200204
}
201205

0 commit comments

Comments
 (0)