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

Remove missed --raw reference in faunadbToCommandError #535

Merged
merged 4 commits into from
Dec 17, 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
42 changes: 23 additions & 19 deletions src/commands/database/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,32 @@ async function createDatabase(argv) {
logger.stdout(argv.name);
}
} catch (e) {
faunaToCommandError(e, (err) => {
if (err instanceof ServiceError && err.code === "constraint_failure") {
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) {
throw new CommandError(
`The database name '${argv.name}' is invalid. Database names must begin with letters and include only letters, numbers, and underscores.`,
{ cause: err },
faunaToCommandError({
err: e,
color: argv.color,
handler: (err) => {
if (err instanceof ServiceError && err.code === "constraint_failure") {
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) {
throw new CommandError(
`The database name '${argv.name}' is invalid. Database names must begin with letters and include only letters, numbers, and underscores.`,
{ cause: err },
);
}
}
throw new CommandError(
`The database '${argv.name}' already exists or one of the provided options is invalid.`,
{ cause: err },
);
}
throw new CommandError(
`The database '${argv.name}' already exists or one of the provided options is invalid.`,
{ cause: err },
);
}
},
});
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/commands/database/delete.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ async function deleteDatabase(argv) {

// We use stderr for messaging and there's no stdout output for a deleted database
logger.stderr(`Database '${argv.name}' was successfully deleted.`);
} catch (e) {
faunaToCommandError(e, (err) => {
if (err instanceof ServiceError && err.code === "document_not_found") {
throw new CommandError(
`Database '${argv.name}' not found. Please check the database name and try again.`,
);
}
} catch (err) {
faunaToCommandError({
err,
color: argv.color,
handler: (err) => {
if (err instanceof ServiceError && err.code === "document_not_found") {
throw new CommandError(
`Database '${argv.name}' not found. Please check the database name and try again.`,
);
}
},
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/database/list.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function listDatabasesWithSecret(argv) {
});
return res.data;
} catch (e) {
return faunaToCommandError(e);
return faunaToCommandError({ err: e, color: argv.color });
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@
throw err;
}

const { color } = argv;
if (argv.apiVersion === "4") {
faunadbToCommandError(err);
faunadbToCommandError({ err, color });
} else {
faunaToCommandError(err);
faunaToCommandError({ err, color });
}
}

Expand Down Expand Up @@ -186,7 +187,7 @@
* @param {string[]} include - The query info fields to include
* @returns {object} An object with the selected query info fields
*/
const pickAndCamelCaseQueryInfo = (response, include) => {

Check warning on line 190 in src/lib/fauna-client.mjs

View workflow job for this annotation

GitHub Actions / lint

Arrow function has a complexity of 11. Maximum allowed is 10
const queryInfo = {};

if (include.includes("txnTs") && response.txn_ts)
Expand Down
29 changes: 15 additions & 14 deletions src/lib/fauna.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -168,34 +168,35 @@ export const formatQueryResponse = (res, opts = {}) => {
* can be provided for different types of errors, and a default error
* message is thrown if no handler is provided. This may be used when we run
* commands on the users behalf and want to provide a more helpful error message.
*
* @param {import("fauna").FaunaError} e - The Fauna error to handle
* @param {(e: import("fauna").FaunaError) => void} [handler] - Optional error handler to handle and throw in
* @param {object} opts
* @param {import("fauna").FaunaError} opts.err - The Fauna error to handle
* @param {(e: import("fauna").FaunaError) => void} [opts.handler] - Optional error handler to handle and throw in
* @param {boolean} [opts.color] - Whether to colorize the error
* @throws {Error} Always throws an error with a message based on the error code or handler response
* @returns {never} This function always throws an error
*/

export const faunaToCommandError = (e, handler) => {
export const faunaToCommandError = ({ err, handler, color }) => {
if (handler) {
handler(e);
handler(err);
}

if (e instanceof ServiceError) {
switch (e.code) {
if (err instanceof ServiceError) {
switch (err.code) {
case "unauthorized":
throw new AuthenticationError({ cause: e });
throw new AuthenticationError({ cause: err });
case "forbidden":
throw new AuthorizationError({ cause: e });
throw new AuthorizationError({ cause: err });
case "permission_denied":
throw new AuthorizationError({ cause: e });
throw new AuthorizationError({ cause: err });
default:
throw new CommandError(formatError(e), { cause: e });
throw new CommandError(formatError(err, { color }), { cause: err });
}
}

if (e instanceof NetworkError) {
throw new CommandError(NETWORK_ERROR_MESSAGE, { cause: e });
if (err instanceof NetworkError) {
throw new CommandError(NETWORK_ERROR_MESSAGE, { cause: err });
}

throw e;
throw err;
};
11 changes: 7 additions & 4 deletions src/lib/faunadb.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,13 @@ export const formatError = (err, opts = {}) => {

/**
* Converts a Fauna HTTP error to a CommandError.
* @param {any} err - The error to convert
* @param {(e: import("fauna").FaunaError) => void} [handler] - Optional error handler to handle and throw in
* @param {object} opts
* @param {any} opts.err - The error to convert
* @param {(e: import("fauna").FaunaError) => void} [opts.handler] - Optional error handler to handle and throw in
* @param {boolean} [opts.color] - Whether to colorize the error
* @returns {void}
*/
export const faunadbToCommandError = (err, handler) => {
export const faunadbToCommandError = ({ err, handler, color }) => {
if (handler) {
handler(err);
}
Expand All @@ -150,7 +153,7 @@ export const faunadbToCommandError = (err, handler) => {
throw new AuthorizationError({ cause: err });
case "BadRequest":
case "NotFound":
throw new CommandError(formatError(err, { raw: true }), { cause: err });
throw new CommandError(formatError(err, { color }), { cause: err });
default:
throw err;
}
Expand Down
14 changes: 7 additions & 7 deletions test/lib/fauna.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("faunaToCommandError", () => {
});

try {
faunaToCommandError(serviceError);
faunaToCommandError({ err: serviceError });
} catch (error) {
expect(error).to.be.instanceOf(AuthenticationError);
expect(error.cause).to.equal(serviceError);
Expand All @@ -35,7 +35,7 @@ describe("faunaToCommandError", () => {
});

try {
faunaToCommandError(serviceError);
faunaToCommandError({ err: serviceError });
} catch (error) {
expect(error).to.be.instanceOf(AuthorizationError);
expect(error.cause).to.equal(serviceError);
Expand All @@ -51,7 +51,7 @@ describe("faunaToCommandError", () => {
});

try {
faunaToCommandError(serviceError);
faunaToCommandError({ err: serviceError });
} catch (error) {
expect(error).to.be.instanceOf(AuthorizationError);
expect(error.cause).to.equal(serviceError);
Expand All @@ -67,7 +67,7 @@ describe("faunaToCommandError", () => {
});

try {
faunaToCommandError(serviceError);
faunaToCommandError({ err: serviceError });
} catch (error) {
expect(error).to.be.instanceOf(CommandError);
expect(error.cause).to.equal(serviceError);
Expand All @@ -78,7 +78,7 @@ describe("faunaToCommandError", () => {
const networkError = new NetworkError("Network failure");

try {
faunaToCommandError(networkError);
faunaToCommandError({ err: networkError });
} catch (error) {
expect(error).to.be.instanceOf(CommandError);
expect(error.message).to.equal(NETWORK_ERROR_MESSAGE);
Expand All @@ -90,7 +90,7 @@ describe("faunaToCommandError", () => {
const genericError = new Error("Generic error");

try {
faunaToCommandError(genericError);
faunaToCommandError({ err: genericError });
} catch (error) {
expect(error).to.equal(genericError);
}
Expand All @@ -111,7 +111,7 @@ describe("faunaToCommandError", () => {
};

try {
faunaToCommandError(serviceError, handler);
faunaToCommandError({ err: serviceError, handler });
} catch (error) {
expect(handlerCalled).to.be.true;
expect(error).to.be.instanceOf(AuthenticationError);
Expand Down
20 changes: 12 additions & 8 deletions test/lib/faunadb.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("faunadbToCommandError", () => {
},
});

expect(() => faunadbToCommandError(faunaError)).to.throw(
expect(() => faunadbToCommandError({ err: faunaError })).to.throw(
AuthenticationError,
);
});
Expand All @@ -29,7 +29,7 @@ describe("faunadbToCommandError", () => {
},
});

expect(() => faunadbToCommandError(faunaError)).to.throw(
expect(() => faunadbToCommandError({ err: faunaError })).to.throw(
AuthorizationError,
);
});
Expand All @@ -41,7 +41,9 @@ describe("faunadbToCommandError", () => {
},
});

expect(() => faunadbToCommandError(faunaError)).to.throw(CommandError);
expect(() => faunadbToCommandError({ err: faunaError })).to.throw(
CommandError,
);
});

it("should convert NotFound error to CommandError", () => {
Expand All @@ -51,13 +53,15 @@ describe("faunadbToCommandError", () => {
},
});

expect(() => faunadbToCommandError(faunaError)).to.throw(CommandError);
expect(() => faunadbToCommandError({ err: faunaError })).to.throw(
CommandError,
);
});

it("should convert network error to CommandError with network message", () => {
const networkError = new TypeError("fetch failed");

expect(() => faunadbToCommandError(networkError)).to.throw(
expect(() => faunadbToCommandError({ err: networkError })).to.throw(
CommandError,
NETWORK_ERROR_MESSAGE,
);
Expand All @@ -70,15 +74,15 @@ describe("faunadbToCommandError", () => {
},
});

expect(() => faunadbToCommandError(faunaError)).to.throw(
expect(() => faunadbToCommandError({ err: faunaError })).to.throw(
faunadb.errors.FaunaHTTPError,
);
});

it("should pass through other errors unchanged", () => {
const genericError = new Error("Generic error");

expect(() => faunadbToCommandError(genericError)).to.throw(Error);
expect(() => faunadbToCommandError({ err: genericError })).to.throw(Error);
});

it("should call optional error handler if provided", () => {
Expand All @@ -89,7 +93,7 @@ describe("faunadbToCommandError", () => {
const error = new Error("Test error");

try {
faunadbToCommandError(error, handler);
faunadbToCommandError({ err: error, handler });
} catch (e) {
// Expected to throw
}
Expand Down
Loading