From fbf65b58f9702c57cfb202cfd2e71f9b1eebaad1 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 31 Aug 2009 18:57:01 +0200 Subject: [PATCH] Use Error exceptions where possible in net.cc and file.cc --- src/file.cc | 2 +- src/net.cc | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/file.cc b/src/file.cc index d905c1db257..6528faee618 100644 --- a/src/file.cc +++ b/src/file.cc @@ -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) diff --git a/src/net.cc b/src/net.cc index c92d9d57da2..a5466ac1145 100644 --- a/src/net.cc +++ b/src/net.cc @@ -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 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 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); @@ -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 exception = Exception::Error(String::New("Socket is not open for writing")); + return ThrowException(exception); } // XXX @@ -424,7 +428,10 @@ Connection::Send (const Arguments& args) } connection->Send(buf, len); - } else return ThrowException(String::New("Bad argument")); + } else { + Local exception = Exception::TypeError(String::New("Bad argument")); + return ThrowException(exception); + } return Undefined(); } @@ -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 exception = Exception::TypeError(String::New("First argument must be a port number")); + return ThrowException(exception); + } String::AsciiValue port(args[0]->ToString()); @@ -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 exception = Exception::Error(String::New(strerror(errno))); + return ThrowException(exception); + } address = AddressDefaultToIPv4(address_list);