elysiajs / eden

Fully type-safe Elysia client
MIT License
174 stars 41 forks source link

Customize eden fetch error type #49

Open wyrd-code opened 9 months ago

wyrd-code commented 9 months ago

I am trying to return a structured error response from the server, so I can return several validation messages at once for for example.

ie with an onError server handler like this

  .onError((context) => {
    const { code, error, set } = context
    switch (code) {
      case 'VALIDATION_ERROR':
        set.status = 422
        return {
          message: error.toString().replace('Error: ', '')
          fields: []
        }
      }
  })

But when a request fails, the eden fetch error.value type is a string:

  const { data, error} = await fetch('/endpoint')

It would be nice to be able to customize the returned error type, so I could access error.value.message and have it properly typed too.