-
First of all I want to state the I have a bit of a nuisance when it comes to the This feels very hacky and could probably be solved a lot better if other properties were exposed to the Example: import { getRegularLogger } from "@/logger";
import { createSafeActionClient } from "next-safe-action";
import { z } from "zod";
export const actionClient = createSafeActionClient({
handleReturnedServerError(error) {
// passing the error to the logging middleware
return {
error,
};
},
defineMetadataSchema() {
return z.object({
actionName: z.string()
});
},
}).use(
async ({
next,
bindArgsClientInputs: [id, stepId],
clientInput,
metadata: { actionName },
}) => {
const logger = await getRegularLogger();
const context = {
StepId: stepId,
StepCode: stepCode,
ApplicationId: id,
};
logger("Information", {
body: {
input: clientInput
stepId,
id,
},
...context,
});
const result = await next({ ctx: null });
if (!result.success) {
const t = await getTranslations("serverError");
logger("Error", {
...context,
validation: {
errors: [result.bindArgsValidationErrors, result.validationErrors],
},
error: JSON.parse(
JSON.stringify(
result.serverError?.error,
Object.getOwnPropertyNames(result.serverError?.error),
),
),
});
// overwriting the error to not bleed it to the client
result.serverError = {
error: "Something went wrong"
};
}
return result;
},
); Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Starting from v7.1.3, both
|
Beta Was this translation helpful? Give feedback.
Starting from v7.1.3, both
handleReturnedServerError
andhandleServerErrorLog
functions get util props as their second argument. More information about that here.handleServerErrorLog
also has access toreturnedError
now, in addition to theoriginalError
, so it's much more flexible.handleServerErrorLog
is there since the beginning, when the library only supported one monolithic middleware defined at the instance level. Now you can handle logging both inside middleware functions and inhandleServerErrorLog
. I think it's f…