Skip to content

Commit f7c7ff0

Browse files
authored
update error handling for js resolvers input path validations (#2782)
1 parent ea28e83 commit f7c7ff0

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

.changeset/five-dancers-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-data': patch
3+
---
4+
5+
Update error handling for js resolvers input path validations
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { beforeEach, describe, it, mock } from 'node:test';
2+
import assert from 'node:assert';
3+
import { resolveEntryPath } from './resolve_entry_path.js';
4+
import fs from 'fs';
5+
void describe('resolveEntryPath', () => {
6+
const testExistentPath = '/existent/path';
7+
const fsMock = mock.method(fs, 'existsSync', (path: string) => {
8+
if (path === testExistentPath) return true;
9+
return false;
10+
});
11+
void beforeEach(() => {
12+
fsMock.mock.resetCalls();
13+
});
14+
void it('should return the same path if it is a string and it exists', () => {
15+
const result = resolveEntryPath(testExistentPath);
16+
assert.strictEqual(result, testExistentPath);
17+
});
18+
19+
void it('should throw error if the path is a string and it does not exist', () => {
20+
assert.throws(() => resolveEntryPath('testNonExistentPath'), {
21+
message: 'Cannot find file at testNonExistentPath',
22+
});
23+
});
24+
});

packages/backend-data/src/resolve_entry_path.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,37 @@ import {
55
import { dirname, join } from 'path';
66
import type { JsResolverEntry } from '@aws-amplify/data-schema-types';
77
import { AmplifyDataError } from './types.js';
8+
import fs from 'fs';
89

910
/**
1011
* Resolve JS Resolver Handler or Sql Reference Handler entry path to absolute path
1112
* @param entry handler entry
1213
* @returns resolved absolute file path
1314
*/
1415
export const resolveEntryPath = (entry: JsResolverEntry): string => {
15-
const unresolvedImportLocationError = new AmplifyUserError<AmplifyDataError>(
16-
'UnresolvedEntryPathError',
17-
{
18-
message:
19-
'Could not determine import path to construct absolute code path from relative path: ' +
20-
JSON.stringify(entry),
21-
resolution: 'Consider using an absolute path instead.',
22-
},
23-
);
24-
2516
if (typeof entry === 'string') {
26-
return entry;
17+
if (fs.existsSync(entry)) {
18+
return entry;
19+
}
20+
throw new AmplifyUserError<AmplifyDataError>('InvalidPathError', {
21+
message: `Cannot find file at ${entry}`,
22+
resolution: `Check that the file exists at ${entry} and is readable`,
23+
});
2724
}
2825

29-
const filePath = new FilePathExtractor(entry.importLine).extract();
30-
if (filePath) {
31-
return join(dirname(filePath), entry.relativePath);
26+
const importPath = new FilePathExtractor(entry.importLine).extract();
27+
28+
if (importPath) {
29+
const filePath = join(dirname(importPath), entry.relativePath);
30+
if (filePath && fs.existsSync(filePath)) {
31+
return filePath;
32+
}
3233
}
3334

34-
throw unresolvedImportLocationError;
35+
throw new AmplifyUserError<AmplifyDataError>('UnresolvedEntryPathError', {
36+
message:
37+
'Could not determine import path to construct absolute code path from relative path: ' +
38+
JSON.stringify(entry),
39+
resolution: 'Consider using an absolute path instead.',
40+
});
3541
};

packages/backend-data/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export type DataProps = {
159159

160160
export type AmplifyDataError =
161161
| 'DefineDataConfigurationError'
162+
| 'InvalidPathError'
162163
| 'InvalidSchemaAuthError'
163164
| 'InvalidSchemaError'
164165
| 'MultipleSingletonResourcesError'

0 commit comments

Comments
 (0)