Skip to content

Commit

Permalink
Trim space from end of libvips error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Mar 24, 2023
1 parent 0af070e commit b55e58f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ namespace sharp {
return EndsWith(str, ".v") || EndsWith(str, ".V") || EndsWith(str, ".vips") || EndsWith(str, ".VIPS");
}

/*
Trim space from end of string.
*/
std::string TrimEnd(std::string const &str) {
return str.substr(0, str.find_last_not_of(" \n\r\f") + 1);
}

/*
Provide a string identifier for the given image type.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ namespace sharp {
bool IsDzZip(std::string const &str);
bool IsV(std::string const &str);

/*
Trim space from end of string.
*/
std::string TrimEnd(std::string const &str);

/*
Provide a string identifier for the given image type.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class MetadataWorker : public Napi::AsyncWorker {
}
Callback().MakeCallback(Receiver().Value(), { env.Null(), info });
} else {
Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, baton->err).Value() });
Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });
}

delete baton->input;
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ class PipelineWorker : public Napi::AsyncWorker {
Callback().MakeCallback(Receiver().Value(), { env.Null(), info });
}
} else {
Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, baton->err).Value() });
Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });
}

// Delete baton
Expand Down
2 changes: 1 addition & 1 deletion src/stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class StatsWorker : public Napi::AsyncWorker {
info.Set("dominant", dominant);
Callback().MakeCallback(Receiver().Value(), { env.Null(), info });
} else {
Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, baton->err).Value() });
Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });
}

delete baton->input;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ describe('Partial image extraction', function () {
.extract({ left: 3000, top: 10, width: 10, height: 10 })
.toBuffer(function (err) {
assert(err instanceof Error);
assert.strictEqual(err.message, 'extract_area: bad extract area\n');
assert.strictEqual(err.message, 'extract_area: bad extract area');
done();
});
});
Expand Down

0 comments on commit b55e58f

Please sign in to comment.