Skip to content

Commit

Permalink
Use Error exceptions where possible in net.cc and file.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Aug 31, 2009
1 parent 3862fda commit fbf65b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/file.cc
Expand Up @@ -26,7 +26,7 @@ using namespace node;
#define ATIME_SYMBOL String::NewSymbol("atime")
#define MTIME_SYMBOL String::NewSymbol("mtime")
#define CTIME_SYMBOL String::NewSymbol("ctime")
#define BAD_ARGUMENTS String::New("Bad argument")
#define BAD_ARGUMENTS Exception::TypeError(String::New("Bad argument"))

static int
AfterClose (eio_req *req)
Expand Down
29 changes: 20 additions & 9 deletions src/net.cc
Expand Up @@ -145,14 +145,17 @@ Connection::Connect (const Arguments& args)
}

if (connection->ReadyState() != EVCOM_INITIALIZED) {
return ThrowException(String::New("Socket is not in CLOSED state."));
Local<Value> exception = Exception::Error(String::New("Socket is not in CLOSED state."));
return ThrowException(exception);
}

assert(connection->stream_.recvfd < 0);
assert(connection->stream_.sendfd < 0);

if (args.Length() == 0)
return ThrowException(String::New("Must specify a port."));
if (args.Length() == 0) {
Local<Value> exception = Exception::TypeError(String::New("First argument must be a port number"));
return ThrowException(exception);
}

String::AsciiValue port_sv(args[0]->ToString());
assert(connection->port_ == NULL);
Expand Down Expand Up @@ -383,7 +386,8 @@ Connection::Send (const Arguments& args)
&& connection->ReadyState() != EVCOM_CONNECTED_WO
)
{
return ThrowException(String::New("Socket is not open for writing"));
Local<Value> exception = Exception::Error(String::New("Socket is not open for writing"));
return ThrowException(exception);
}

// XXX
Expand Down Expand Up @@ -424,7 +428,10 @@ Connection::Send (const Arguments& args)
}
connection->Send(buf, len);

} else return ThrowException(String::New("Bad argument"));
} else {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
return ThrowException(exception);
}

return Undefined();
}
Expand Down Expand Up @@ -598,8 +605,10 @@ Server::Listen (const Arguments& args)

HandleScope scope;

if (args.Length() == 0)
return ThrowException(String::New("Must give at least a port as argument."));
if (args.Length() == 0) {
Local<Value> exception = Exception::TypeError(String::New("First argument must be a port number"));
return ThrowException(exception);
}

String::AsciiValue port(args[0]->ToString());

Expand Down Expand Up @@ -631,8 +640,10 @@ Server::Listen (const Arguments& args)
struct addrinfo *address = NULL,
*address_list = NULL;
int r = getaddrinfo(strlen(host) ? host : NULL, *port, &server_tcp_hints, &address_list);
if (r != 0)
return ThrowException(String::New(strerror(errno)));
if (r != 0) {
Local<Value> exception = Exception::Error(String::New(strerror(errno)));
return ThrowException(exception);
}

address = AddressDefaultToIPv4(address_list);

Expand Down

0 comments on commit fbf65b5

Please sign in to comment.