question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Discussion: Custom `Err` types in `Result`

See original GitHub issue

Original Comment

This function could produce an Ok.

const a = 2 as unknown as Error
const res = Err(a) // Not a string, not an Error, but TypeScript thinks so
console.log(res.isOk() // true
console.log(res.isErr() // false

Perhaps a generic isErrorLike(input: any): input is Error function could be created which checks for characteristics of objects like Errors could be used in this function as well as the Result.isErr function.

interface ErrorKind {
	message: string;
}
type ErrorLike = Error | ErrorKind;

function isErrorLike(input: any): input is ErrorLike {
	return (
		input instanceof Error ||
		(input &&
			typeof input === 'object' &&
			(Object.prototype.isPrototypeOf.call(Error, input) ||
				typeof input.message === 'string'))
	);
}

I am kind of concerned about forcing Error as the only type here, as then we get undefined behavior at runtime anyway… should it throw if anything not Error or string is passed?

I think Rust allows any value to be passed for E in Result<T, E>, and JS allows us to throw any value, so perhaps there could be some other signal that what’s constructed is an error?

Perhaps an optional type: 'Err' | 'Ok' = 'Ok' as an arg, and then always pass 'Err' from function Err?

Such a type value could then also be used in the Result.isOk and Result.isErr functions to avoid prototype chain lookups which are currently used to decide Result.isErr. Might provide a performance bump, and it’s how I do it in my private lib.

_Originally posted by @aaronhuggins in https://github.com/OliverBrotchie/optionals/pull/7#discussion_r1008324662_

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aaronhugginscommented, Oct 31, 2022

FYI, I’m a sucker for JSON serializable errors when given the chance.

0reactions
OliverBrotchiecommented, Nov 1, 2022

FYI, I’m a sucker for JSON serializable errors when given the chance.

I am too! 😆

This does however preclude AmbiguousError from implement’ing Error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rust: Structuring and handling errors in 2020 - Nick Groenen
Custom result types like failure::Fail may not compose well with other parts of your user's code and force them to learn yet another...
Read more >
How do you define custom `Error` types in Rust?
The crate custom_error allows the definition of custom error types with less boilerplate than what was proposed above: custom_error!{
Read more >
Allow for returning custom error type in Result #1004 - GitHub
Having to change my function signatures to return either a JsValue or some new wasm-bindgen specific error type is extra code that I'd...
Read more >
Rust error management: lessons learned - DEV Community ‍ ‍
All I/O Rust standard library functions use a specific error type called Result which seems to be confusing at first sight.
Read more >
Custom Error types in Rust and the ? operator - Medium
A custom error type. A logical idea would be to import diesel::result::Error and implement Rocket's Responder trait on it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found