Skip to content
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

Handle Invalid Database Names #510

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/commands/database/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,21 @@ async function createDatabase(argv) {
} catch (e) {
if (e instanceof FaunaError) {
throwForError(e, {
onConstraintFailure: () =>
`Constraint failure: The database '${argv.name}' already exists or one of the provided options is invalid.`,
onConstraintFailure: (err) => {
const cf = err.constraint_failures;
if (cf && cf.length > 0) {
const nameIsInvalidIdentifier = cf.some(
(failure) =>
failure?.paths?.length === 1 &&
failure?.paths?.[0]?.[0] === "name" &&
failure?.message === "Invalid identifier.",
);
if (nameIsInvalidIdentifier) {
return `Constraint failure: The database name '${argv.name}' is invalid. Database names must begin with letters and include only letters, numbers, and underscores.`;
}
}
return `Constraint failure: The database '${argv.name}' already exists or one of the provided options is invalid.`;
},
});
}
throw e;
Expand Down
16 changes: 16 additions & 0 deletions test/database/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ describe("database create", () => {
expectedMessage:
"Constraint failure: The database 'testdb' already exists or one of the provided options is invalid.",
},
{
error: new ServiceError({
error: {
code: "constraint_failure",
message: "whatever",
constraint_failures: [
{
paths: [["name"]],
message: "Invalid identifier.",
},
],
},
}),
expectedMessage:
"Constraint failure: The database name 'testdb' is invalid. Database names must begin with letters and include only letters, numbers, and underscores.",
},
{
error: new ServiceError({
error: { code: "unauthorized", message: "whatever" },
Expand Down
Loading