kit icon indicating copy to clipboard operation
kit copied to clipboard

Export `ActionFailure` to allow `instanceof`

Open probablykasper opened this issue 1 year ago • 0 comments

Describe the problem

I have some shared logic for parsing user input, but it's not that convenient to deal with success/error types.

Describe the proposed solution

ActionFailure is a class, but SvelteKit exposes it as an interface. It would be nice to simply return fail() and do instanceof ActionFailure:

function parse(value: T | null) {
	if (!value) {
		return fail(400, { message: 'Too high' })
	}
	// ...
	return value
}

// Action:
const result = parse('test')
if (result instanceof ActionFailure) {
	return result
}

Alternatives considered

  • Creating my own class
  • Returning a result type like { value: T, error: null } | { value: null, error: string }
  • Use the Error class message as an intermediate, if I only need an error string

Importance

nice to have

Additional Information

No response

probablykasper avatar Aug 25 '24 11:08 probablykasper