Skip to content

Commit 3bd9c2c

Browse files
committed
fix: incorrect multiple calls of handleServerError with thrown errors
fix #370
1 parent c0dcae0 commit 3bd9c2c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/next-safe-action/src/action-builder.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ export function actionBuilder<
7373
const parsedInputDatas: any[] = [];
7474
const frameworkErrorHandler = new FrameworkErrorHandler();
7575

76+
// Track if server error has been handled.
77+
let serverErrorHandled = false;
78+
7679
if (withState) {
7780
// Previous state is placed between bind args and main arg inputs, so it's always at the index of
7881
// the bind args schemas + 1. Get it and remove it from the client inputs array.
@@ -242,6 +245,11 @@ export function actionBuilder<
242245
middlewareResult.bindArgsParsedInputs = parsedInputDatas.slice(0, -1);
243246
}
244247
} catch (e: unknown) {
248+
// Only handle server errors once. If already handled, rethrow to bubble up.
249+
if (serverErrorHandled) {
250+
throw e;
251+
}
252+
245253
// If error is `ActionServerValidationError`, return `validationErrors` as if schema validation would fail.
246254
if (e instanceof ActionServerValidationError) {
247255
const ve = e.validationErrors as ValidationErrors<IS>;
@@ -256,6 +264,9 @@ export function actionBuilder<
256264
})
257265
);
258266
} else {
267+
// Mark that we're handling the server error to prevent multiple calls.
268+
serverErrorHandled = true;
269+
259270
// If error is not an instance of Error, wrap it in an Error object with
260271
// the default message.
261272
const error = isError(e) ? e : new Error(DEFAULT_SERVER_ERROR_MESSAGE);

0 commit comments

Comments
 (0)