Skip to content

Commit

Permalink
fs: replace bad_args macro with concrete error msg
Browse files Browse the repository at this point in the history
Instead of throwing an error with `Bad arguments` the fs methods
return a concrete error message. If an argument is missing, the
methods throw an error with the information, what is missing.
In case of a type mismatch, they throw an error with some hints,
what datatype is expected.

PR-URL: #2495
Reviewed-By: Trevor Norris <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
  • Loading branch information
romankl authored and rvagg committed Aug 23, 2015
1 parent 64a8f30 commit 023386c
Showing 1 changed file with 47 additions and 34 deletions.
81 changes: 47 additions & 34 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ using v8::Value;

#define TYPE_ERROR(msg) env->ThrowTypeError(msg)

#define THROW_BAD_ARGS TYPE_ERROR("Bad argument")

#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)

class FSReqWrap: public ReqWrap<uv_fs_t> {
Expand Down Expand Up @@ -306,7 +304,7 @@ static void Access(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(env->isolate());

if (args.Length() < 2)
return THROW_BAD_ARGS;
return TYPE_ERROR("path and mode are required");
if (!args[0]->IsString())
return TYPE_ERROR("path must be a string");
if (!args[1]->IsInt32())
Expand All @@ -326,9 +324,10 @@ static void Access(const FunctionCallbackInfo<Value>& args) {
static void Close(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 1 || !args[0]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 1)
return TYPE_ERROR("fd is required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");

int fd = args[0]->Int32Value();

Expand Down Expand Up @@ -559,9 +558,10 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
static void FStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 1 || !args[0]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 1)
return TYPE_ERROR("fd is required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");

int fd = args[0]->Int32Value();

Expand Down Expand Up @@ -678,9 +678,10 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
static void FTruncate(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 2 || !args[0]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 2)
return TYPE_ERROR("fd and length are required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");

int fd = args[0]->Int32Value();

Expand All @@ -706,9 +707,10 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 1 || !args[0]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 1)
return TYPE_ERROR("fd is required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");

int fd = args[0]->Int32Value();

Expand All @@ -722,9 +724,10 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
static void Fsync(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 1 || !args[0]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 1)
return TYPE_ERROR("fd is required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");

int fd = args[0]->Int32Value();

Expand Down Expand Up @@ -772,9 +775,12 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
static void MKDir(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 2)
return TYPE_ERROR("path and mode are required");
if (!args[0]->IsString())
return TYPE_ERROR("path must be a string");
if (!args[1]->IsInt32())
return TYPE_ERROR("mode must be an integer");

node::Utf8Value path(env->isolate(), args[0]);
int mode = static_cast<int>(args[1]->Int32Value());
Expand Down Expand Up @@ -990,9 +996,12 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
static void Read(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 2 || !args[0]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 2)
return TYPE_ERROR("fd and buffer are required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");
if (!Buffer::HasInstance(args[1]))
return TYPE_ERROR("Second argument needs to be a buffer");

int fd = args[0]->Int32Value();

Expand All @@ -1003,10 +1012,6 @@ static void Read(const FunctionCallbackInfo<Value>& args) {

char * buf = nullptr;

if (!Buffer::HasInstance(args[1])) {
return env->ThrowError("Second argument needs to be a buffer");
}

Local<Object> buffer_obj = args[1]->ToObject(env->isolate());
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
Expand Down Expand Up @@ -1043,9 +1048,13 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
static void Chmod(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 2)
return TYPE_ERROR("path and mode are required");
if (!args[0]->IsString())
return TYPE_ERROR("path must be a string");
if (!args[1]->IsInt32())
return TYPE_ERROR("mode must be an integer");

node::Utf8Value path(env->isolate(), args[0]);
int mode = static_cast<int>(args[1]->Int32Value());

Expand All @@ -1063,9 +1072,13 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
static void FChmod(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 2 || !args[0]->IsInt32() || !args[1]->IsInt32()) {
return THROW_BAD_ARGS;
}
if (args.Length() < 2)
return TYPE_ERROR("fd and mode are required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");
if (!args[1]->IsInt32())
return TYPE_ERROR("mode must be an integer");

int fd = args[0]->Int32Value();
int mode = static_cast<int>(args[1]->Int32Value());

Expand Down

0 comments on commit 023386c

Please sign in to comment.