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

feat: merge server error handling functions into handleServerError #257

Merged
merged 3 commits into from
Sep 4, 2024

Conversation

TheEdoRan
Copy link
Owner

@TheEdoRan TheEdoRan commented Sep 4, 2024

Code in this PR 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({
  handleServerErrorLog(error) {
    console.error("my custom error log:", error.message);
  },
  handleReturnedServerError(error) {
    return {
      message: error.message,
    };
  },
});

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

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

const ac = createSafeActionClient({
  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;
  },
});

Copy link

vercel bot commented Sep 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-safe-action-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 4, 2024 0:25am
next-safe-action-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 4, 2024 0:25am

@TheEdoRan TheEdoRan changed the title refactor: merge server error handling functions into handleServerError feat: merge server error handling functions into handleServerError Sep 4, 2024
Copy link

github-actions bot commented Sep 4, 2024

🎉 This PR is included in version 7.9.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant