New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix when you create-amplify absolute path, raised error #1071
fix when you create-amplify absolute path, raised error #1071
Conversation
🦋 Changeset detectedLatest commit: 7cc3407 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Note to other reviewers: this change was discussed in #857 and got moved to new PR. |
try { | ||
await fsp.mkdir(projectRoot, { recursive: true }); | ||
} catch (err) { | ||
if (path.isAbsolute(projectRoot)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what conditions would a projectRoot
not be an absolute path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Amplifiyer
I can't think of a case at the moment where projectRoot not be an absolute path, but I put it in explicitly.
Is it necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there is no situation where projectRoot
wouldn't be an absolute path than this if
statement is irrelevant and we should throw an AmplifyError
with the right messaging here.
cc: @edwardfoyle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the upstream validation, I would say we can remove this conditional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you feedback !!! @edwardfoyle @Amplifiyer
I try to do this task!
- remove condition
- add
AmplifyError
(change test case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came up with three error pattern.
should select plan3 right?
// plan1: use printer
try {
await fsp.mkdir(projectRoot, {recursive: true});
} catch (err) {
printer.log(
`Failed to create directory at ${projectRoot}. Ensure this is the correct path and you have write permissions to this location.`,
LogLevel.ERROR
);
throw err;
}
// plan2: direct throw new Error
try {
await fsp.mkdir(projectRoot, {recursive: true});
} catch (err) {
throw new Error(`Failed to create directory at ${projectRoot}. Ensure this is the correct path and you have write permissions to this location.`);
}
// plan3: use AmplifyUserError
import {AmplifyUserError} from '@aws-amplify/platform-core';
try {
await fsp.mkdir(projectRoot, {recursive: true});
} catch (err) {
throw new AmplifyUserError('MultipleSingletonResourcesError', {
message:
`Failed to create directory at ${projectRoot}. Ensure this is the correct path and you have write permissions to this location.`,
resolution: 'Change relative path or Absolute path under root',
});
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes plan3 is correct. Make the error name something like ProjectDirectoryCreateError
and change the message and resolution to something like
try {
await fsp.mkdir(projectRoot, {recursive: true});
} catch (err) {
throw new AmplifyUserError('MultipleSingletonResourcesError', {
message:
`Failed to create project directory`,
resolution: 'Ensure that ${projectRoot} is the correct path and you have write permissions to this location.',
});
}
653ecb3
I modified the test code and AmplifyError with reference to the following.
|
(err: Error) => { | ||
const sampleStderr = `some random stderr | ||
before the actual error message | ||
${util.inspect(err, { depth: null })} | ||
and some after the error message`; | ||
|
||
const actual = AmplifyError.fromStderr(sampleStderr); | ||
assert.deepStrictEqual(actual?.name, expectedError.name); | ||
assert.deepStrictEqual( | ||
actual?.classification, | ||
expectedError.classification | ||
); | ||
assert.deepStrictEqual(actual?.message, expectedError.message); | ||
assert.deepStrictEqual(actual?.details, expectedError.details); | ||
assert.deepStrictEqual(actual?.cause?.name, expectedError.cause?.name); | ||
assert.deepStrictEqual( | ||
actual?.cause?.message, | ||
expectedError.cause?.message | ||
); | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need all this. Just assert on the err
that the name
, message
and resolution
is correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Amplifiyer
thank you feedback!!!!!
I try change test case
await assert.rejects( | ||
() => getProjectRoot(), | ||
(err: Error) => { | ||
const sampleStderr = `some random stderr | ||
before the actual error message | ||
${util.inspect(err, { depth: null })} | ||
and some after the error message`; | ||
|
||
const actual = AmplifyError.fromStderr(sampleStderr); | ||
assert.deepStrictEqual(actual?.name, expectedError.name); | ||
assert.deepStrictEqual(actual?.message, expectedError.message); | ||
assert.deepStrictEqual(actual?.resolution, expectedError.resolution); | ||
return true; | ||
} | ||
); | ||
assert.equal(fsMkDirSyncMock.mock.callCount(), 1); | ||
assert.equal( | ||
fsMkDirSyncMock.mock.calls[0].arguments[0], | ||
path.resolve(userInput) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like below
await assert.rejects( | |
() => getProjectRoot(), | |
(err: Error) => { | |
const sampleStderr = `some random stderr | |
before the actual error message | |
${util.inspect(err, { depth: null })} | |
and some after the error message`; | |
const actual = AmplifyError.fromStderr(sampleStderr); | |
assert.deepStrictEqual(actual?.name, expectedError.name); | |
assert.deepStrictEqual(actual?.message, expectedError.message); | |
assert.deepStrictEqual(actual?.resolution, expectedError.resolution); | |
return true; | |
} | |
); | |
assert.equal(fsMkDirSyncMock.mock.callCount(), 1); | |
assert.equal( | |
fsMkDirSyncMock.mock.calls[0].arguments[0], | |
path.resolve(userInput) | |
); | |
await assert.throws( | |
() => getProjectRoot(), | |
expectedError | |
); | |
assert.equal(fsMkDirSyncMock.mock.callCount(), 1); | |
assert.equal( | |
fsMkDirSyncMock.mock.calls[0].arguments[0], | |
path.resolve(userInput) | |
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made a few modifications.
at d7226b6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came up with idea.
const expectedError = new AmplifyUserError('ProjectDirectoryCreateError', {
message: `Failed to create project directory`,
resolution: `Ensure that ${
userInput
} is the correct path and you have write permissions to this location.`,
});
fsMkDirSyncMock.mock.mockImplementationOnce(() => {
throw expectedError;
});
await assert.rejects(() => getProjectRoot(), {
name: expectedError.name,
message: expectedError.message,
});
Problem
**Issue number, if available: **
fixes: #827
Related pull requests: #857
Changes
Corresponding docs PR, if applicable:
when you do create-amplify absolute path (ex. '/user/test'), raised error and log.
Validation
Checklist
run-e2e
label set.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.