Skip to content

[feature] Document how to use EvlogError with Tanstack Start Server Functions #405

Description

@piero-vic

Problem or motivation

There is no mention in the docs how we could use EvlogError with Tanstack Start Server Functions. It seems to be common practice to just throw from these functions. However, when throwing an EvlogError properties like why and fix are not sent to the client. By default, errors are sent just with the message property.

Proposed feature

We could document the use of createSerializationAdapter and add it to the examples. This function allows us to define custom types that can be serialized. I've added something like this to src/start.ts and it's been working fine.

import { createSerializationAdapter } from '@tanstack/react-router'
import { createStart } from '@tanstack/react-start'
import { createError, EvlogError, parseError } from 'evlog'

const evlogErrorAdapter = createSerializationAdapter({
  key: 'EvlogError',
  test: (error: unknown): error is EvlogError => error instanceof EvlogError,
  toSerializable: (error) => {
    const { raw, ...rest } = parseError(error)
    return rest
  },
  fromSerializable: (value) => createError(value)
})

export const startInstance = createStart(() => {
  return {
    serializationAdapters: [evlogErrorAdapter]
  }
})

It is worth noting that these adapters are not yet documented.

Possible implementation (optional)

No response

Alternatives you’ve considered (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions