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.

Expose exception types for verification of real time data in application code.

See original GitHub issue

This is just a vague musing. I’d really like to hear some differing points of view.

Would anyone else find it useful to have more control over how sanctuary responds to type errors?

Here are some scenario’s I’m imagining:

Decorating an existing API with def and defining RecordType’s for valid request/responses and then intercepting type errors to provide a user facing API Error e.g. a required property, or an invalid property type.

It’d would also be helpful for unit testing libraries that sit on top of sanctuary-def. Currently I do some string searching of error messages in sum-type, in order to verify a particular error is thrown. It would be a lot more precise if the various exceptions were documented and exposed and considered part of the API surface (e.g. semver).

Maybe sanctuary-def could allow rejection behaviour to be specified by the user. E.g. on error, you could write the particular error to an error stream. Maybe if there was a low level def that allowed you to pass in a resolve/reject function. The default def would throw. But we could optionally create def functions that reject via an Either, a Task or a Future in the event a Type error occurs.

It’d also be great if sanctuary-def could expose as much information as possible about the type error on the Error object. If a property was missing on a Record type, which ones? If there was an unexpected value, what was it?

This may be well beyond the scope of the library. I’d really love to hear criticisms, or any other related ideas.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
davidchamberscommented, Mar 15, 2017

I see a lot of value in providing ways to reuse the library’s validation logic.

Have you played with the validate method at all? It may satisfy some of your requirements:

> $.RecordType({x: $.FiniteNumber, y: $.FiniteNumber}).validate({x: 0, y: Infinity})
Left({propPath: ['y'], value: Infinity})
0reactions
JAForbescommented, Apr 30, 2017

I think there’s a few different use cases. For generating error messages for users of libraries built on to of sanctuary-def, I think validate’s behaviour is probably adequate. For usage in unit tests, it’d be nice to have a built in like:

> var assertType = (T, a) => def('assertType', {}, [$.Type, T], i => i )(T, a)
undefined
> assertType( $.Number, '4' )

TypeError: Invalid value

assertType :: Type -> Number
                      ^^^^^^
                        1

1)  "4" :: String

The value at position 1 is not a member of ‘Number’.

I mean that signature or error message isn’t really accurate, I want it to say, the second argument should be a member of the type provided as the first argument.

In any case, that particular error message would be more useful than validates output when asserting a value is of a particular type.

But if you have suggestions for a better error message I’d love to hear it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Error Handling - Microsoft Learn
When an error occurs in the application, the Application_Error handler is called. In this handler, the last exception is retrieved and reviewed.
Read more >
Exception handling in Net: Advanced exceptions | Hexacta
Our HAT team give us a tour around advanced exceptions in .NET, in order to learn about their pitfalls and best practices when...
Read more >
What is Data Validation and When Do You Do It?
The card information is both validated and verified. As soon as the data is ingested and validated, the result is sent back to...
Read more >
Error Handling Flaws - Information and How to Fix - Veracode
Error messages in web apps provide valuable insights into issues and problems. It is critical to have proper error handling to prevent security...
Read more >
owasp-mastg/0x05d-Testing-Data-Storage.md at master
Sensitive data is vulnerable when it is not properly protected by the app that is persistently storing it. The app may be able...
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