Skip to content

Commit

Permalink
provide a more actionable error message on invalid database names (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryfauna authored Dec 11, 2024
1 parent e25ef6d commit fb4fdd2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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

0 comments on commit fb4fdd2

Please sign in to comment.