From 823a443321360e089e9c6d169a6cc116e3fb50b2 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Fri, 2 Dec 2011 00:02:51 +0100 Subject: [PATCH] Rename FSError to UVException and move to node.cc --- src/node.cc | 76 +++++++++++++++++++++++++++++++++++++++ src/node.h | 6 ++++ src/node_file.cc | 93 ++++-------------------------------------------- 3 files changed, 88 insertions(+), 87 deletions(-) diff --git a/src/node.cc b/src/node.cc index fbe1ad0e40..9189f8b255 100644 --- a/src/node.cc +++ b/src/node.cc @@ -800,6 +800,82 @@ Local ErrnoException(int errorno, } +static const char* get_uv_errno_string(int errorno) { + uv_err_t err; + memset(&err, 0, sizeof err); + err.code = (uv_err_code)errorno; + return uv_err_name(err); +} + + +static const char* get_uv_errno_message(int errorno) { + uv_err_t err; + memset(&err, 0, sizeof err); + err.code = (uv_err_code)errorno; + return uv_strerror(err); +} + + +// hack alert! copy of ErrnoException, tuned for uv errors +Local UVException(int errorno, + const char *syscall, + const char *msg, + const char *path) { + static Persistent syscall_symbol; + static Persistent errpath_symbol; + static Persistent code_symbol; + + if (syscall_symbol.IsEmpty()) { + syscall_symbol = NODE_PSYMBOL("syscall"); + errno_symbol = NODE_PSYMBOL("errno"); + errpath_symbol = NODE_PSYMBOL("path"); + code_symbol = NODE_PSYMBOL("code"); + } + + if (!msg || !msg[0]) + msg = get_uv_errno_message(errorno); + + Local estring = String::NewSymbol(errno_string(errorno)); + Local message = String::NewSymbol(msg); + Local cons1 = String::Concat(estring, String::NewSymbol(", ")); + Local cons2 = String::Concat(cons1, message); + + Local e; + + Local path_str; + + if (path) { +#ifdef _WIN32 + if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) { + path_str = String::Concat(String::New("\\\\"), String::New(path + 8)); + } else if (strncmp(path, "\\\\?\\", 4) == 0) { + path_str = String::New(path + 4); + } else { + path_str = String::New(path); + } +#else + path_str = String::New(path); +#endif + + Local cons3 = String::Concat(cons2, String::NewSymbol(" '")); + Local cons4 = String::Concat(cons3, path_str); + Local cons5 = String::Concat(cons4, String::NewSymbol("'")); + e = Exception::Error(cons5); + } else { + e = Exception::Error(cons2); + } + + Local obj = e->ToObject(); + + // TODO errno should probably go + obj->Set(errno_symbol, Integer::New(errorno)); + obj->Set(code_symbol, estring); + if (path) obj->Set(errpath_symbol, path_str); + if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall)); + return e; +} + + #ifdef _WIN32 Local WinapiErrnoException(int errorno, const char* syscall, diff --git a/src/node.h b/src/node.h index cb58a220bb..38d3c621bf 100644 --- a/src/node.h +++ b/src/node.h @@ -192,6 +192,12 @@ NODE_EXTERN v8::Local ErrnoException(int errorno, const char *syscall = NULL, const char *msg = "", const char *path = NULL); + +NODE_EXTERN v8::Local UVException(int errorno, + const char *syscall = NULL, + const char *msg = NULL, + const char *path = NULL); + #ifdef _WIN32 NODE_EXTERN v8::Local WinapiErrnoException(int errorno, const char *syscall = NULL, const char *msg = "", diff --git a/src/node_file.cc b/src/node_file.cc index 2cc6fe580a..c6af67db2a 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -56,11 +56,6 @@ static Persistent errno_symbol; static Persistent buf_symbol; static Persistent oncomplete_sym; -Local FSError(int errorno, - const char *syscall = NULL, - const char *msg = NULL, - const char *path = NULL); - #ifdef _LARGEFILE_SOURCE static inline int IsInt64(double x) { @@ -91,12 +86,12 @@ static void After(uv_fs_t *req) { // If the request doesn't have a path parameter set. if (!req->path) { - argv[0] = FSError(req->errorno); + argv[0] = UVException(req->errorno); } else { - argv[0] = FSError(req->errorno, - NULL, - NULL, - static_cast(req->path)); + argv[0] = UVException(req->errorno, + NULL, + NULL, + static_cast(req->path)); } } else { // error value is empty or null for non-error. @@ -210,82 +205,6 @@ struct fs_req_wrap { }; -const char* errno_string(int errorno) { - uv_err_t err; - memset(&err, 0, sizeof err); - err.code = (uv_err_code)errorno; - return uv_err_name(err); -} - - -const char* errno_message(int errorno) { - uv_err_t err; - memset(&err, 0, sizeof err); - err.code = (uv_err_code)errorno; - return uv_strerror(err); -} - - -// hack alert! copy of ErrnoException in node.cc, tuned for uv errors -Local FSError(int errorno, - const char *syscall, - const char *msg, - const char *path) { - static Persistent syscall_symbol; - static Persistent errpath_symbol; - static Persistent code_symbol; - - if (syscall_symbol.IsEmpty()) { - syscall_symbol = NODE_PSYMBOL("syscall"); - errno_symbol = NODE_PSYMBOL("errno"); - errpath_symbol = NODE_PSYMBOL("path"); - code_symbol = NODE_PSYMBOL("code"); - } - - if (!msg || !msg[0]) - msg = errno_message(errorno); - - Local estring = String::NewSymbol(errno_string(errorno)); - Local message = String::NewSymbol(msg); - Local cons1 = String::Concat(estring, String::NewSymbol(", ")); - Local cons2 = String::Concat(cons1, message); - - Local e; - - Local path_str; - - if (path) { -#ifdef _WIN32 - if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) { - path_str = String::Concat(String::New("\\\\"), String::New(path + 8)); - } else if (strncmp(path, "\\\\?\\", 4) == 0) { - path_str = String::New(path + 4); - } else { - path_str = String::New(path); - } -#else - path_str = String::New(path); -#endif - - Local cons3 = String::Concat(cons2, String::NewSymbol(" '")); - Local cons4 = String::Concat(cons3, path_str); - Local cons5 = String::Concat(cons4, String::NewSymbol("'")); - e = Exception::Error(cons5); - } else { - e = Exception::Error(cons2); - } - - Local obj = e->ToObject(); - - // TODO errno should probably go - obj->Set(errno_symbol, Integer::New(errorno)); - obj->Set(code_symbol, estring); - if (path) obj->Set(errpath_symbol, path_str); - if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall)); - return e; -} - - #define ASYNC_CALL(func, callback, ...) \ FSReqWrap* req_wrap = new FSReqWrap(); \ int r = uv_fs_##func(uv_default_loop(), &req_wrap->req_, \ @@ -300,7 +219,7 @@ Local FSError(int errorno, int result = uv_fs_##func(uv_default_loop(), &req_wrap.req, __VA_ARGS__, NULL); \ if (result < 0) { \ int code = uv_last_error(uv_default_loop()).code; \ - return ThrowException(FSError(code, #func, "", path)); \ + return ThrowException(UVException(code, #func, "", path)); \ } #define SYNC_REQ req_wrap.req