-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting data out of an Error, easier classifying unknown values, "Type" module thoughts #102
Comments
I built some helper functions and here is how I use it to get the details I want from an exception... } catch {
| _ as exn => {
let isUserNotFound =
exn
->Error.fromException
->Option.flatMap(ErrorExtras.get(_, "code"))
->Option.flatMap(Unknown.toString)
->Option.filter(i => i === "auth/user-not-found")
->Option.isSome |
Some experimenting... {"favorites": null}
->Object.get("favorites")
->Option.map(i => i->Array.length)
->Option.getWithDefault(0)
->Console.log // Run-time error of "Cannot read properties of null"
{"count": null}
->Object.get("count")
->Option.map(i => i + 6)
->Option.getWithDefault(0)
->Console.log // Outputs 6, null + 6 = 6 |
What about an Then people can decide themselves how safe they want to play it after using that. |
I'm thinking this over. Is a JavaScript exception ALWAYS an object? If I throw a primitive does it get wrapped? |
No, you can throw anything in JS. Though everything in JS is an object in the OO sense. Also, interestingly, I believe the stack trace is recorded when the |
I think the naming convention would be |
This situation with getting a custom value out of I'd like to prototype a Are you interested in seeing my ideas on this? |
See my comment here: #108 (review) |
Btw type |
We have a similar usecase when interacting with module CustomError = {
type t = { message: string, code: string, otherCustomProperty: bool }
external ofJsExn: Js.Exn.t => t = "%identity"
}
// somewhere else
switch await someAsyncStuff {
| result => Js.Console.log(result)
| exception Js.Exn.Error(err) =>
let customErr = err->CustomError.ofJsExn
customErr.code->Js.Console.log
} This is obviously unsafe to do. But any way to get some property of an object (in js representation) after the fact, that it was returned as an "unspecific" type would be. The only safe way for us, I see, is to actually not use the promise api of |
Is solved in #146 |
Is solved in #145 |
I'm trying to do something simple. I caught a Firebase exception and want to access the
code
property that is a string. Firebase errors have code properties with contents like "auth/email-password-invalid". How can I do that? Here is one awkward way. Is there a simpler way?I think it would be useful to access arbitrary properties on
Error.t
just like I can onObject.t
. I also want a much easier way to work with those values and not have to switch onType.Classify.classify
.First idea is to add an
Error.get
function just like we haveObject.get
. This would be very convenient.Second idea is to add convenience methods for classify like this. All these do is wrap the classify function with a Switch.
The third idea is to rename the
Type
module toUnknown
and have anunknown
type. When we have functions that return some unknown thing we could label them explicitly asunknown
and the developer will then know they can use theUnknown
module to classify and make sense of it. The rescript-struct module defines asunknown
type that is used for serialization output. https://github.com/DZakh/rescript-struct#sserializewith TypeScript has anunknown
type. TheObject.get
function returns anoption<'a>
which is too permissive because you can Option.map it and start treating it like something it is not and get a runtime error. SoObject.get
should return anoption<unknown>
and force the developer to safely inspect and classify it.Finally and some random thoughts on the
Type
module.bigint
.Symbol.t
- it is aType.Classify.symbol
toString
. Why nottypeof
? Wouldn't that be more efficient?Classify
sub-module just to hold theclassify
function. Couldn't this all be flattened out?The text was updated successfully, but these errors were encountered: