Skip to content

fix(start): Send error messages from server functions - #2625

Closed
danecando wants to merge 3 commits into
TanStack:mainfrom
danecando:fix/start/server-fn-error
Closed

fix(start): Send error messages from server functions#2625
danecando wants to merge 3 commits into
TanStack:mainfrom
danecando:fix/start/server-fn-error

Conversation

@danecando

@danecando danecando commented Oct 23, 2024

Copy link
Copy Markdown
Contributor

Removes some unnecessary logs and passes error messages from server function errors. Passes error messages directly to errors thrown from the client instead of putting a json payload in the message.

This should align better with whats shown in the docs. It seems unsafe to send along server stack traces though.

Aside from special redirect and notFound errors, server functions can throw any custom error. These errors will be serialized and sent to the client as a JSON response along with a 500 status code.

import { createServerFn } from '@tanstack/start'

export const doStuff = createServerFn('GET', async () => {
  throw new Error('Something went wrong!')
})

// Usage
function Test() {
  try {
    await doStuff()
  } catch (error) {
    console.error(error)
    // {
    //   message: "Something went wrong!",
    //   stack: "Error: Something went wrong!\n    at doStuff (file:///path/to/file.ts:3:3)"
    // }
  }
}

Would like to support returning other error responses without having them thrown on the client side as well. Curious how other people feel about that.

@nx-cloud

nx-cloud Bot commented Oct 27, 2024

Copy link
Copy Markdown
Contributor

☁️ Nx Cloud Report

CI is running/has finished running commands for commit f25e09d. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

@schiller-manuel

Copy link
Copy Markdown
Collaborator

can you please add tests to the start e2e tests?

@pkg-pr-new

pkg-pr-new Bot commented Oct 27, 2024

Copy link
Copy Markdown

Open in Stackblitz

More templates

@tanstack/create-router

pnpm add https://pkg.pr.new/@tanstack/create-router@2625

@tanstack/history

pnpm add https://pkg.pr.new/@tanstack/history@2625

@tanstack/eslint-plugin-router

pnpm add https://pkg.pr.new/@tanstack/eslint-plugin-router@2625

@tanstack/react-cross-context

pnpm add https://pkg.pr.new/@tanstack/react-cross-context@2625

@tanstack/react-router

pnpm add https://pkg.pr.new/@tanstack/react-router@2625

@tanstack/react-router-with-query

pnpm add https://pkg.pr.new/@tanstack/react-router-with-query@2625

@tanstack/router-arktype-adapter

pnpm add https://pkg.pr.new/@tanstack/router-arktype-adapter@2625

@tanstack/router-cli

pnpm add https://pkg.pr.new/@tanstack/router-cli@2625

@tanstack/router-devtools

pnpm add https://pkg.pr.new/@tanstack/router-devtools@2625

@tanstack/router-generator

pnpm add https://pkg.pr.new/@tanstack/router-generator@2625

@tanstack/router-plugin

pnpm add https://pkg.pr.new/@tanstack/router-plugin@2625

@tanstack/router-valibot-adapter

pnpm add https://pkg.pr.new/@tanstack/router-valibot-adapter@2625

@tanstack/router-vite-plugin

pnpm add https://pkg.pr.new/@tanstack/router-vite-plugin@2625

@tanstack/router-zod-adapter

pnpm add https://pkg.pr.new/@tanstack/router-zod-adapter@2625

@tanstack/start

pnpm add https://pkg.pr.new/@tanstack/start@2625

@tanstack/start-vite-plugin

pnpm add https://pkg.pr.new/@tanstack/start-vite-plugin@2625

@tanstack/virtual-file-routes

pnpm add https://pkg.pr.new/@tanstack/virtual-file-routes@2625

commit: f25e09d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants