Skip to content

v7.9.0

Compare
Choose a tag to compare
@github-actions github-actions released this 04 Sep 12:31
· 33 commits to main since this release
c900720

7.9.0 (2024-09-04)

Features

  • merge server error handling functions into handleServerError (#257) (c900720)

Description

v7.9.0 merges the functionality of handleServerErrorLog and handleReturnedServerError functions into a single optional initialization function called handleServerError. This change has been made because having two functions for server error handling is unnecessary, you can easily manage both logging and returned error within a single function.

Upgrade guide

Suppose you have this code using next-safe-action < 7.9.0:

import { createSafeActionClient } from "next-safe-action";

const actionClient = createSafeActionClient({
  // handles logging
  handleServerErrorLog(error) {
    console.error("my custom error log:", error.message);
  },
  // handles returned shape
  handleReturnedServerError(error) {
    return {
      message: error.message,
    };
  },
});

With next-safe-action >= 7.9.0 it becomes:

import { createSafeActionClient } from "next-safe-action";

const ac = createSafeActionClient({
  // handles both logging and returned shape
  handleServerError(error) {
    console.error("my custom error log:", error.message);
    return {
      message: error.message,
    };
  },
});

So, minimal refactoring is required, and the action client creation is cleaner this way.


Note

Even if you want to change just the logging mechanism, you still have to return an error shape from handleServerError, otherwise the resulting type would be void.

So, if you want for instance keep the default error message as the returned server error and just update the console logging, you can do it like this:

import { createSafeActionClient, DEFAULT_SERVER_ERROR_MESSAGE } from "next-safe-action";

const ac = createSafeActionClient({
  handleServerError(error) {
    console.error("my custom error log:", error.message);
    return DEFAULT_SERVER_ERROR_MESSAGE;
  },
});