snafu | Easily assign underlying errors into domain-specific errors | Architecture library
kandi X-RAY | snafu Summary
kandi X-RAY | snafu Summary
Situation Normal: All Fouled Up. SNAFU is a library to easily assign underlying errors into domain-specific errors while adding context. Please see the documentation and the user's guide for a full description.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of snafu
snafu Key Features
snafu Examples and Code Snippets
Community Discussions
Trending Discussions on snafu
QUESTION
I have implemented a parser in Rust that can return different types of errors. Previously I had implemented it using different Structs without fields that implemented the std::error::Error
trait. The issue is that I encountered two problems:
- The parse function returns a
Box
, which I find stilted to check what error was returned since I have to fall back to the.downcast_ref::()
method instead of simply being able to use a match and check what error variant was returned. - Now I want all errors to report, with a custom message, line number, and position of the text where the error occurs. This item is easily solved with Structs, but I still fall into the previous problem.
For both reasons is that I want to implement it with Enums. My question is: knowing that all variants have the fields pos, line, and message, should I repeat the three fields for all Enum variants? Is there another better way to represent the problem?
What I had in mind was the following code block, but honestly having to repeat the same code for all variants seems a bit far-fetched (maybe this could be solved with a macro?):
...ANSWER
Answered 2021-Nov-21 at 20:25Maybe I'm oversimplifying, but two approaches come to my mind:
Use a
struct
as follows:
QUESTION
I am writing a batch script that does a myriad of things. The one part I am stuck on is copying files/file structure from a location to my final image. The file structure looks something like this
...ANSWER
Answered 2021-May-18 at 17:33You can use XCOPY, which is way better than COPY.
XCOPY is NOT a third party command, there's no need for software. It was added back in 1986 (MS-DOS 3.30, correct me if I'm wrong), so every Windows OS has it.
Command would be: xcopy /y /h /i /e foo bar
Which will:
- Copy all directories structure, including empty directories (
/e
) - It won't prompt for confirmation
- Hidden files will be also copied.
QUESTION
I'm trying to use the snafu crate for error handling, but keep getting erros that my Error enum struct is missing the 'source' and that IntoError
is not implimented for Error
:
ANSWER
Answered 2021-Mar-06 at 20:49It's because when you're constructing LoadGallery
, you're attempting to construct Error::LoadGallery
. You then get a compile error saying "missing source
", because the Error::LoadGallery
variant has a source
field. Fixing it is straight forward, you just need to change which LoadGallery
you import.
QUESTION
I'm building an app with rust. Using --cargo run --release
successfully compiles the app and runs it, bringing up the GUI window for the app. However, when I manually open target/release/MyApp.exe
, nothing happens. Checked when myapp.exe
was last modifies shows that running --cargo run --release
is updating the app.
I'm on windows 10 so I added "x86_64-pc-windows-msvc" as the build target.
...ANSWER
Answered 2021-Mar-06 at 04:21Might just be a shot in the dark, but are you opening the exe
using Windows Explorer?
Open up a cmd.exe
window and try running it from there.
QUESTION
ANSWER
Answered 2021-Feb-05 at 08:40The #[derive(Snafu)]
attribute creates "context selectors" for each enum variant. That means that Error::IncorrectInputType
refers to the variant, while IncorrectInputType
is a generated struct which has the fail()
method.
The fix is to use this selector instead of the enum:
QUESTION
I have an error type that impls the Error
trait, and it wraps an underlying error cause, so the source
method returns Some(source)
. I want to know whether the Display
impl on my error type should include a description of that source error, or not.
I can see two options:
- Yes, include
source
inDisplay
output, e.g. "Error opening database: No such file"
This makes it easy to print a whole error chain just by formatting with "{}"
but impossible to only display the error itself without the underlying chain of source errors. Also it makes the source
method a bit pointless and gives client code no choice on how to format separation between each error in the chain. Nevertheless this choice seems common enough in example code I have found.
- No, just print the error itself e.g. "Error opening database" and leave it to client code to traverse and display
source
if it wants to include that in the output.
This gives client code the choice of whether to display just the surface error or the whole chain, and in the latter case how to format separation between each error in the chain. It leaves client code with the burden of iterating through the chain, and I haven't yet fallen upon a canonical utility for conveniently formatting an error chain from errors that each only Display
themselves excluding source
. (So of course I have my own.)
The snafu crate (which I really like) seems to hint at favoring option 2, in that an error variant with a source
field but no display
attribute defaults to formatting Display
output that does not include source
.
Maybe my real question here is: What is the purpose of the source
method? Is it to make formatting error chains more flexible? Or should Display
really output everything that should be user-visible about an error, and source
is just there for developer-visible purposes?
I would love to see some definitive guidance on this, ideally in the documentation of the Error
trait.
ANSWER
Answered 2020-Sep-19 at 11:45The two options of whether to print the source error on a Display
implementation creates two schools of design.
At the moment, neither one is more idiomatic than the other, although opinions towards both ways exist, and one could eventually find a community-wide consensus in the future, or may already be so in the context of a particular project or code base.
The Rust API guidelines do not present an opinion about Display
in errors, other than C-GOOD-ERR, which merely states that the error type's Display
message should be "lowercase without trailing punctuation, and typically concise". There is a pending proposal to update this guideline, instructing developers to exclude source
in their Display
impl. Again, the proposal does not exist without some friction.
What can be done here and now is objectively state the key differences between the two, while clarifying a few possible misconceptions in the way.
1. Yes, includesource
on your Display
impl
Example with SNAFU:
QUESTION
I was testing my regular expression skill at this site.
One of the questions was to come up with an expression that would match "fu, dofu, snafu" but not "futz, fusillade, functional, discombobulated".
I thought this an easy task and entered my answer as "fu$". To my surprise, the answer was not accepted. I then looked at the question more closely and found this phrase: "$ not allowed." Now, I'm stuck.
What should the regular expression be?
...ANSWER
Answered 2020-Aug-02 at 04:32It seems that you want to just match word which end in fu
, but not necessarily words that only have fu
as a substring without also ending in fu
. Consider using:
QUESTION
I've recently started learning Rust, and I'm currently trying to create a small API; I've made my own struct for an API Response and enum for an Error:
...ANSWER
Answered 2020-Mar-30 at 13:52I've run into this error as well. The following is mentioned in the API Docs:
A type implementing
Responder
should implement theDebug
trait when possible. This is because theResponder
implementation forResult
requires itsErr
type to implementDebug
. Therefore, a type implementingDebug
can more easily be composed.
That means you must implement Debug, as you use ApiResponse
as an Err
type.
QUESTION
I'm trying to use Get-ChildItem
in powershell (I'm using the ls
alias) to return only files within a specific folder structure.
As an example, say I'm trying to list all files whose names end in .foobar.txt
that reside within a folder called src
, which in turn resides in a folder called even
. I've tried using the command ls -r *\even\src\*.foobar.txt | % FullName
. I thought the first wildcard *
in the path would allow any path that ends in \even\src\
and that the second one would match only files that end in .foobar.txt
. But it isn't returning all the files I expect. Here's the structure of the directory I'm testing it in:
ANSWER
Answered 2020-Jan-20 at 23:04Break it up into 2 steps. First recurse all the directories, filtering out the ones you want, then pipe that result through another get-childitem
that filters out the filenames you want:
QUESTION
I'm unable to actually access the member(s) in this undocumented class: BingMapsRESTToolkit.Response
Code in various places (e.g.)Bing Maps REST Services Toolkit - Access Value Outside of Delegate seem to copy the non-working example from MSDN.
For example, this method has no access to the array of Snapped coordinates contained in the Response object. The line where rez is assigned to the cast of the response gives us a null.
...ANSWER
Answered 2020-Jan-17 at 21:48Turns out the solution is fairly straight-forward, but at least for me, isn't inferred in the (lack of) documentation for the BingMapsRESTToolkit project.
The Response object can take on a variety of object types depending on the initial request.
In order to get access to the SnappedPoints array, the Response has to be cast as a SnapToRoadResponse type, such like:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install snafu
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page