Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: replace bad_args macro with concrete error msg #2495

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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