Skip to content

Commit 023386c

Browse files
romanklrvagg
authored andcommitted
fs: replace bad_args macro with concrete error msg
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]>
1 parent 64a8f30 commit 023386c

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

src/node_file.cc

+47-34
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ using v8::Value;
4747

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

50-
#define THROW_BAD_ARGS TYPE_ERROR("Bad argument")
51-
5250
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)
5351

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

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

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

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

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

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

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

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

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

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

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

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

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

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

725-
if (args.Length() < 1 || !args[0]->IsInt32()) {
726-
return THROW_BAD_ARGS;
727-
}
727+
if (args.Length() < 1)
728+
return TYPE_ERROR("fd is required");
729+
if (!args[0]->IsInt32())
730+
return TYPE_ERROR("fd must be a file descriptor");
728731

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

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

775-
if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsInt32()) {
776-
return THROW_BAD_ARGS;
777-
}
778+
if (args.Length() < 2)
779+
return TYPE_ERROR("path and mode are required");
780+
if (!args[0]->IsString())
781+
return TYPE_ERROR("path must be a string");
782+
if (!args[1]->IsInt32())
783+
return TYPE_ERROR("mode must be an integer");
778784

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

993-
if (args.Length() < 2 || !args[0]->IsInt32()) {
994-
return THROW_BAD_ARGS;
995-
}
999+
if (args.Length() < 2)
1000+
return TYPE_ERROR("fd and buffer are required");
1001+
if (!args[0]->IsInt32())
1002+
return TYPE_ERROR("fd must be a file descriptor");
1003+
if (!Buffer::HasInstance(args[1]))
1004+
return TYPE_ERROR("Second argument needs to be a buffer");
9961005

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

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

10041013
char * buf = nullptr;
10051014

1006-
if (!Buffer::HasInstance(args[1])) {
1007-
return env->ThrowError("Second argument needs to be a buffer");
1008-
}
1009-
10101015
Local<Object> buffer_obj = args[1]->ToObject(env->isolate());
10111016
char *buffer_data = Buffer::Data(buffer_obj);
10121017
size_t buffer_length = Buffer::Length(buffer_obj);
@@ -1043,9 +1048,13 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
10431048
static void Chmod(const FunctionCallbackInfo<Value>& args) {
10441049
Environment* env = Environment::GetCurrent(args);
10451050

1046-
if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsInt32()) {
1047-
return THROW_BAD_ARGS;
1048-
}
1051+
if (args.Length() < 2)
1052+
return TYPE_ERROR("path and mode are required");
1053+
if (!args[0]->IsString())
1054+
return TYPE_ERROR("path must be a string");
1055+
if (!args[1]->IsInt32())
1056+
return TYPE_ERROR("mode must be an integer");
1057+
10491058
node::Utf8Value path(env->isolate(), args[0]);
10501059
int mode = static_cast<int>(args[1]->Int32Value());
10511060

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

1066-
if (args.Length() < 2 || !args[0]->IsInt32() || !args[1]->IsInt32()) {
1067-
return THROW_BAD_ARGS;
1068-
}
1075+
if (args.Length() < 2)
1076+
return TYPE_ERROR("fd and mode are required");
1077+
if (!args[0]->IsInt32())
1078+
return TYPE_ERROR("fd must be a file descriptor");
1079+
if (!args[1]->IsInt32())
1080+
return TYPE_ERROR("mode must be an integer");
1081+
10691082
int fd = args[0]->Int32Value();
10701083
int mode = static_cast<int>(args[1]->Int32Value());
10711084

0 commit comments

Comments
 (0)