I am currently implementing a low-level API for file handling, for the Mozilla Platform. This API is based on libc/winapi (which do not have exceptions) but itself throws exceptions in case of errors.
This causes any number of "amusing"/error-prone situations, in which the
implementation of the API combines functions that do not throw
exceptions (because these errors are expected to be handled immediately)
and functions that do throw exceptions (because we do not want to forget
reporting errors), |errno|-based error checking, etc.
For instance, let us consider the following (otherwise rather simple)
function |move|, whose role is to rename/move a file or a directory,
possibly across devices.
Function move(from, to, options)
1. - If |options| specifies that we should not overwrite
2. - Attempt to |open| for reading |to|
3. - If the call succeeds, close file and bail out
(destination exists).
4. - If the call fails due to insufficient rights for opening |to|,
bail out (destination exists).
5. - If the call fails due to |to| being ill-formed or too long,
bail out (client error).
6. - Call libc |rename(from, to)|
7. - If the call succeeds, we are done.
8. - If the call fails due to files not being on the same device,
proceed.
9. - If the call fails for any other reason, bail out (propagate
error as received).
10. - Call function |copy|.
11. - If the call fails, bail out (error as received).
12. - Call function |remove|.
13. - If the call fails, bail out (error as received).
14. - We have succeeded
This simple function already illustrates the following cases:
- Call to |open|:
- catch and ignore most errors;
- propagate some errors;
- turn success into an error.
- Call to |rename|:
- catch and ignore one error;
- propagate all other errors;
- success is a success.
- Calls to |copy| and |remove|:
- propagate all errors
Now, I have not encountered yet a scenario in which errors raised by
|rename| are handled/ignored in a fine-grained way, but this will
certainly happen.
Cheers,
David
--
David Rajchenbach-Teller, PhD
Performance Team, Mozilla
signature.asc
Description: OpenPGP digital signature
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
