diff --git a/src/commands/database/create.mjs b/src/commands/database/create.mjs index e90a72e5..1a2d5762 100644 --- a/src/commands/database/create.mjs +++ b/src/commands/database/create.mjs @@ -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; diff --git a/test/database/create.mjs b/test/database/create.mjs index bba25aca..f16dd411 100644 --- a/test/database/create.mjs +++ b/test/database/create.mjs @@ -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" },