Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Rename FSError to UVException and move to node.cc
  • Loading branch information
piscisaureus authored and Bert Belder committed Dec 2, 2011
1 parent 1ad30a2 commit 823a443
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 87 deletions.
76 changes: 76 additions & 0 deletions src/node.cc
Expand Up @@ -800,6 +800,82 @@ Local<Value> 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<Value> UVException(int errorno,
const char *syscall,
const char *msg,
const char *path) {
static Persistent<String> syscall_symbol;
static Persistent<String> errpath_symbol;
static Persistent<String> 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<String> estring = String::NewSymbol(errno_string(errorno));
Local<String> message = String::NewSymbol(msg);
Local<String> cons1 = String::Concat(estring, String::NewSymbol(", "));
Local<String> cons2 = String::Concat(cons1, message);

Local<Value> e;

Local<String> 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<String> cons3 = String::Concat(cons2, String::NewSymbol(" '"));
Local<String> cons4 = String::Concat(cons3, path_str);
Local<String> cons5 = String::Concat(cons4, String::NewSymbol("'"));
e = Exception::Error(cons5);
} else {
e = Exception::Error(cons2);
}

Local<Object> 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<Value> WinapiErrnoException(int errorno,
const char* syscall,
Expand Down
6 changes: 6 additions & 0 deletions src/node.h
Expand Up @@ -192,6 +192,12 @@ NODE_EXTERN v8::Local<v8::Value> ErrnoException(int errorno,
const char *syscall = NULL,
const char *msg = "",
const char *path = NULL);

NODE_EXTERN v8::Local<v8::Value> UVException(int errorno,
const char *syscall = NULL,
const char *msg = NULL,
const char *path = NULL);

#ifdef _WIN32
NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(int errorno,
const char *syscall = NULL, const char *msg = "",
Expand Down
93 changes: 6 additions & 87 deletions src/node_file.cc
Expand Up @@ -56,11 +56,6 @@ static Persistent<String> errno_symbol;
static Persistent<String> buf_symbol;
static Persistent<String> oncomplete_sym;

Local<Value> FSError(int errorno,
const char *syscall = NULL,
const char *msg = NULL,
const char *path = NULL);


#ifdef _LARGEFILE_SOURCE
static inline int IsInt64(double x) {
Expand Down Expand Up @@ -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<const char*>(req->path));
argv[0] = UVException(req->errorno,
NULL,
NULL,
static_cast<const char*>(req->path));
}
} else {
// error value is empty or null for non-error.
Expand Down Expand Up @@ -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<Value> FSError(int errorno,
const char *syscall,
const char *msg,
const char *path) {
static Persistent<String> syscall_symbol;
static Persistent<String> errpath_symbol;
static Persistent<String> 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<String> estring = String::NewSymbol(errno_string(errorno));
Local<String> message = String::NewSymbol(msg);
Local<String> cons1 = String::Concat(estring, String::NewSymbol(", "));
Local<String> cons2 = String::Concat(cons1, message);

Local<Value> e;

Local<String> 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<String> cons3 = String::Concat(cons2, String::NewSymbol(" '"));
Local<String> cons4 = String::Concat(cons3, path_str);
Local<String> cons5 = String::Concat(cons4, String::NewSymbol("'"));
e = Exception::Error(cons5);
} else {
e = Exception::Error(cons2);
}

Local<Object> 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_, \
Expand All @@ -300,7 +219,7 @@ Local<Value> 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
Expand Down

0 comments on commit 823a443

Please sign in to comment.