Trying to better coding, trying to raise an error instead of panic! [SOLVED]

Hello to all of the people of this amazing language

First of all sorry for my english

I have a program in Rust, that is downloading a chapter of a comic in a webpage

I have doint first in the sync way, after that in async way and now i will try to learn more concepts about the errors and panic in rust.

In the last week, i have a problem, that many times when i download the chapter, some images are less than 195 bytes
Ok, no problem i have done a check, after i downloaded the image and write it, check the size of the file and if is ok, continue iterating throuth the links of the images, and if the size is less, delete the directory and panic.

This works 100% of the time, but because the web is very slow, many times i see something like this:

https://mangatown.com/manga/ryuu_no_nanakuni_to_minashigo_no_juana/c012/

Fichero muy pequeño menor o igual 195 bytes, borrar directorio
Se ha borrado el directorio
thread 'main' panicked at 'explicit panic', src/main.rs:43:5
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

It's okey rust have delete the directory and have done a panic! to exit the program..

But this got me thinking that is a better way of impleting this, i will try to explain,

I will transform my function to get an image to -> Result<(), Error>
This way i can raise and error, to get it, and delete the directory without panic (if this goes well)

i'm trying something like this:

But i have one problem i want to be easy.

i see that i have to iterate through the images and with .collect use to see the case of and image less that 195bytes (and in the first image corrupted this will stop) i have to delete the directory

but my problem is that rust is telling my that Error
cannot find value fichero_pequeño in this scope
--> src/main.rs:328:8
|
328 | Err(fichero_pequeño)
| ^^^^^^^^^^^^^^^ not found in this scope

i have to create an especific error to raise it, and checked in the superior function or i can have another way of solve it?

Many thanks to all

Hi,

I want to share a few ideas:

fichero_pequeño is not defined anywhere in the code. That's why the compiler doesn't know what this is.
I suppose you would like to indicate that the file is too small. There is several ways to to do that, but first we need to ask ourselves: "What type is this?"

Your method returns Result<(), std::error::Error>. However std::error::Error is a trait, not really a type. While true that dyn Error is a type, I don't think it's what you really want here. You probably want to return a type here that implements Error.

In itself, Result will accept any type, so we could just return Result<(), &'static str> and you could do Err( "fichero_pequeño" ), note the quotes. However as you can see in the documentation, that does not implement std::error::Error.

But why would you want to return something that implements std::error::Error? The main reason is that it allows composing errors. A function might return errors of several types, and they could be combined in one return type by returning something like: Result<(), Box< dyn Error + Send + Sync >. Now we can use the ? operator to easily bail out of the function and the return type will cover any type that implements std::error::Error. The ? operator will even box it for us. I have only found the edition 2018 guide as documentation for the ? operator, but that doesn't explain it will box it. The way this works is that the ? operator will call into on the type, and that there is a blanket impl for From<T> for Box<T>.

So in the end, for a leaf application (as opposed to a library), you don't have to care about this to much just using strings in results if fine.

Ok, this get's a bit long. I just wanted point out that your code is rather complex. You might want to simplify things by not parsing urls manually, but using a crate that does this for you. The url crate is the defacto standard way of doing this I think.

Ok, i get the idea if i will remain easy i will go to the solution of
err("fichero_pequeño ")
the other solution i see it's better but more complex to do it well, because the combined of my errors and anothers ones.

Can you post an example to parse the url with the url crate, think that this program was created very long ago (the very first version at least two years ago and the documentacion and the crates were evoluting)

And how obtain for example the next link:

Read Otoyomegatari Manga - Read Otoyomegatari Online at MangaTown.com in this webpage

Any comment to do a more "rust" way it's welcome

There is examples on the documentation for the url crate. Are they not helpful for you? You probably want the query_pairs method.

Many thanks, little by little, first i will try to don't panic in this case with the error("my_error")

and after that i will try to clean my code using this url crate

i change the topic to solved because now i can search for the solution by miself and learn in the progress.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.