Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src-ts/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class CephError extends Error {
}

static normalize(error: unknown): CephError {
if (error instanceof CephError) return error;
// instanceof also matches error-like objects (see hasInstance), which still have to be turned into a CephError
if (error instanceof Error && CephError.prototype.isPrototypeOf(error)) return error as CephError;
if (error && typeof error === "object" && "code" in error && "operation" in error) {
const value = error as Error & {code: number; errno?: number; codeName?: string; operation: string; resource?: string};
return new CephError(value.message, {code: value.code, errno: value.errno ?? Math.abs(value.code), codeName: value.codeName, operation: value.operation, resource: value.resource, cause: error});
Expand Down
12 changes: 11 additions & 1 deletion src/io_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,17 @@ Napi::Value IoContextWrap::Append(const Napi::CallbackInfo& info) {
}
Napi::Value IoContextWrap::Read(const Napi::CallbackInfo& info) {
auto oid = string_arg(info, 0, "oid"); size_t length = static_cast<size_t>(uint64_arg(info[1], "length")); uint64_t offset = uint64_arg(info[2], "offset"); auto state = in_flight(state_);
return async_value<ReadBytes>(info.Env(), "rados_read", oid, [state, oid, length, offset](ReadBytes& out) { char* buffer = out.allocate(length); int rc = state->use([&](rados_ioctx_t h) { return rados_read(h, oid.c_str(), buffer, length, offset); }); if (rc >= 0) out.size = static_cast<size_t>(rc); return rc < 0 ? rc : 0; }, [](Napi::Env env, ReadBytes& out) { return out.to_buffer(env); });
return async_value<ReadBytes>(info.Env(), "rados_read", oid, [state, oid, length, offset](ReadBytes& out) {
char* buffer = out.allocate(length);
int rc = state->use([&](rados_ioctx_t h) {
// the OSD reads to the end of the object for a length of 0, which does not fit the empty buffer, so a read
// of 0 bytes only checks that the object exists
if (!length) { uint64_t size = 0; time_t modified = 0; return rados_stat(h, oid.c_str(), &size, &modified); }
return rados_read(h, oid.c_str(), buffer, length, offset);
});
if (rc >= 0) out.size = length ? static_cast<size_t>(rc) : 0;
return rc < 0 ? rc : 0;
}, [](Napi::Env env, ReadBytes& out) { return out.to_buffer(env); });
}
Napi::Value IoContextWrap::Remove(const Napi::CallbackInfo& info) {
auto oid = string_arg(info, 0, "oid"); auto state = in_flight(state_); return async_void(info.Env(), "rados_remove", oid, [state, oid] { return state->use([&](rados_ioctx_t h) { return rados_remove(h, oid.c_str()); }); });
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/rados-rbd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ test("compound write checks compare the stored value and the version", {skip: !e
await io.remove("checked");
});

test("a read of 0 bytes returns an empty Buffer, or ENOENT for a missing object", {skip: !enabled}, async () => {
await using io = cluster.ioContext(pool);
await io.writeFull("zero-length", Buffer.from("data"));
const empty = await io.read("zero-length", 0n);
assert.ok(Buffer.isBuffer(empty));
assert.equal(empty.length, 0);
assert.equal((await io.read("zero-length", 0n, 2n)).length, 0);
await assert.rejects(io.read("zero-length-missing", 0n), (error: unknown) => error instanceof CephError && error.errno === 2);
await io.remove("zero-length");
});

test("RBD image, I/O, metadata, snapshots, resize, and trash", {skip: !enabled}, async () => {
await using io = cluster.ioContext(pool);
const rbd = io.rbd();
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ test("recognizes structured errors emitted by the native addon", () => {
assert.equal(CephError.normalize(error).resource, "object");
});

test("normalize() turns error-like objects into a CephError and keeps real ones", () => {
const plain = Object.assign(new Error("busy"), {code: -16, operation: "rados_ioctx_set_namespace"});
const normalized = CephError.normalize(plain);
assert.equal(Object.getPrototypeOf(normalized), CephError.prototype);
assert.equal(normalized.errno, 16);
assert.equal(normalized.code, -16);
assert.equal(normalized.message, "busy");
assert.equal(normalized.operation, "rados_ioctx_set_namespace");
assert.equal(normalized.cause, plain);

const real = new CephError("busy", {code: -16, errno: 16, operation: "op"});
assert.equal(CephError.normalize(real), real);
assert.throws(() => CephError.normalize(new Error("other")), /other/);
});

test("native errors name their errno", async () => {
await assert.rejects(Cluster.connect({configFile: "/nonexistent/node-librados/ceph.conf", parseEnvironment: false}), (error: unknown) => {
assert.ok(error instanceof CephError);
Expand Down
Loading