From 7c004e794c4b7f9c7eb0979cf1c8d608992afb1a Mon Sep 17 00:00:00 2001
From: Robert HH <robert@hammelrath.com>
Date: Fri, 27 May 2016 22:28:00 +0200
Subject: [PATCH] extmod/vfs_fat*: Replace text error messages by POSIX error
 numbers.

These changes are in line with similar changes in other modules, and
with standard Python interface.
---
 extmod/vfs_fat.c      | 34 +++++++++++++++-------------------
 extmod/vfs_fat_misc.c |  5 +++--
 2 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c
index 2404b4aad..d2a0e247a 100644
--- a/extmod/vfs_fat.c
+++ b/extmod/vfs_fat.c
@@ -79,12 +79,11 @@ STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
     const char *path = mp_obj_str_get_str(path_in);
     // TODO check that path is actually a file before trying to unlink it
     FRESULT res = f_unlink(path);
-    switch (res) {
-        case FR_OK:
-            return mp_const_none;
-        default:
-            // TODO: standard errno's
-            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path));
+    if (res == FR_OK) {
+        return mp_const_none;
+    } else {
+        nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
+            MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
     }
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
@@ -94,11 +93,11 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_
     const char *old_path = mp_obj_str_get_str(path_in);
     const char *new_path = mp_obj_str_get_str(path_out);
     FRESULT res = f_rename(old_path, new_path);
-    switch (res) {
-        case FR_OK:
-            return mp_const_none;
-        default:
-            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error renaming file '%s' to '%s'", old_path, new_path));
+    if (res == FR_OK) {
+        return mp_const_none;
+    } else {
+        nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
+            MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
     }
 
 }
@@ -108,14 +107,11 @@ STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
     (void)vfs_in;
     const char *path = mp_obj_str_get_str(path_o);
     FRESULT res = f_mkdir(path);
-    switch (res) {
-        case FR_OK:
-            return mp_const_none;
-        case FR_EXIST:
-            // TODO should be FileExistsError
-            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "File exists: '%s'", path));
-        default:
-            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path));
+    if (res == FR_OK) {
+        return mp_const_none;
+    } else {
+        nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
+            MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
     }
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c
index 9b03f940e..23fe4be88 100644
--- a/extmod/vfs_fat_misc.c
+++ b/extmod/vfs_fat_misc.c
@@ -54,8 +54,9 @@ mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) {
 
     res = f_opendir(&dir, path);                       /* Open the directory */
     if (res != FR_OK) {
-        // TODO should be mp_type_FileNotFoundError
-        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path));
+        nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
+          MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
+
     }
 
     mp_obj_t dir_list = mp_obj_new_list(0, NULL);
-- 
GitLab