Skip to content

feat: filesystem error handling #1006

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

wassup05
Copy link
Contributor

A derived type fs_error containg an integer code and a fixed-length string is added for concise error handling

This is kind of a stripped down version of state_type already present in the stdlib, syntax is also kept very similar... except the introduction of code to store integers returned by C functions such as GetLastError (Windows API), global variables like errno (Posix platforms)

Do let me know your thoughts

@perazz
Copy link
Member

perazz commented Jul 4, 2025

The current stdlib implementation already has a filesystem error handle:

integer(ilp),parameter,public :: STDLIB_FS_ERROR = -5_ilp

It is handled by the library's error class. Most libraries have a unique error handler, pointing to different error types like in our case. Because extending the error class would introduce a lot of boilerplate, is there a clear advantage for going in that direction rather than what stdlib is currently offering?

My understanding is that the current implementation would store platform-dependent error code, that I would refrain from doing. Instead, IMHO platform wrappers would be useful to standardize error messages such as was done for linear algebra, cf. #1013.

In my view, a more useful developer extension would be to expose less verbose pre-built error functions for the user.
Instead of coding

err0 = state_type(STDLIB_FS_ERROR,'Cannot delete',path,'- is a directory')

one could have a wrapper

    !> Wrap STDLIB_FS_ERROR error variable creation
    pure type(state_type) function FS_ERROR(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10, &
                                                     a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)

       !> Optional rank-agnostic arguments
       class(*),optional,intent(in),dimension(..) :: a1,a2,a3,a4,a5,a6,a7,a8,a9,a10, &
                                                     a11,a12,a13,a14,a15,a16,a17,a18,a19,a20 
       
       FS_ERROR = state_type(STDLIB_FS_ERROR,a1,a2,a3,....,a20)
       
    end function FS_ERROR

so that coding FS errors becomes much easier and more clear

err0 = FS_ERROR('Cannot delete',path,'- is a directory')

same for all the high-level error types. This would be visually similar to having an fs_error extended class, clear syntax, but without all the bloat.

@wassup05
Copy link
Contributor Author

wassup05 commented Jul 4, 2025

My understanding is that the current implementation would store platform-dependent error code, that I would refrain from doing. Instead, IMHO platform wrappers would be useful to standardize error messages

As an example, on my main system (linux) in errno.h there are 135 errors defined such as EACCES, ENOTDIR etc and these are NOT the same across all platforms, meaning the macro defining EACCES could be defined using a different integer... It is simply not feasible to cover all these cases separately, That's why I feel giving out the code of the error to the user (as an integer preferably) would be very beneficial as in the user has a way at least to figure out what is happening, using his OS specifications etc instead of probably a vague Unkown error occured in case the error is too uncommon.

ofcourse user friendly messages are required and for that you might want to look at #1011 where I have given out both the code and the message as a string, directly binding to the CRT function strerr which is available on both Windows and Linux, which converts the errno to a char*

And of course having useful developer extensions as you pointed out would be desirable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants