Skip to content

Commit

Permalink
fix: use Error instead of string for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-cucu committed Mar 4, 2024
1 parent 2e482cd commit 0f2871f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
32 changes: 22 additions & 10 deletions api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,15 @@ class Client {
response = await this._admin.login(args);
} catch (error) {
if (error === INVALIDCREDENTIALS_ERROR) {
throw `response
Have you been granted permission to a model on this controller?`;
throw new Error(
"Have you been granted permission to a model on this controller?"
);
} else if (response === PERMISSIONDENIED_ERROR) {
throw `response
Ensure that you've been given 'login' permission on this controller.`;
throw new Error(
"Ensure that you've been given 'login' permission on this controller."
);
} else {
throw error;
throw toError(error);
}
}
const dischargeRequired =
Expand All @@ -308,7 +310,9 @@ class Client {
if (dischargeRequired) {
if (!this._bakery) {
reject(
"macaroon discharge required but no bakery instance provided"
new Error(
"macaroon discharge required but no bakery instance provided"
)
);
return;
}
Expand All @@ -321,7 +325,14 @@ class Client {
return resolve(this.login(credentials, clientVersion));
};
const onFailure = (err: string | MacaroonError) => {
reject("macaroon discharge failed: " + err);
reject(
new Error(
"macaroon discharge failed: " +
(err instanceof Object && "Message" in err
? err.Message
: err)
)
);
};
this._bakery.discharge(dischargeRequired, onSuccess, onFailure);
return;
Expand All @@ -330,7 +341,7 @@ class Client {
} catch (error) {
const errorMessage = error instanceof Error ? error.message : error;
if (errorMessage !== REDIRECTION_ERROR) {
reject(error);
reject(toError(error));
return;
}
// This is a model redirection error, fetch the redirection information.
Expand All @@ -339,7 +350,6 @@ class Client {
reject(new RedirectionError(info));
return;
} catch (error) {
// add util to transform to Error.
reject(toError(error));
return;
}
Expand Down Expand Up @@ -374,13 +384,15 @@ class Client {
const REDIRECTION_ERROR = "redirection required";
const INVALIDCREDENTIALS_ERROR = "invalid entity name or password";
const PERMISSIONDENIED_ERROR = "permission denied";
class RedirectionError {
class RedirectionError extends Error {
servers: RedirectInfoResult["servers"];
caCert: string;

constructor(info: RedirectInfoResult) {
super(REDIRECTION_ERROR);
this.servers = info.servers;
this.caCert = info["ca-cert"];
Object.setPrototypeOf(this, RedirectionError.prototype);
}
}

Expand Down
8 changes: 5 additions & 3 deletions api/tests/test-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ describe("connect", () => {
},
version: 3,
});
expect(error).toBe(
"macaroon discharge required but no bakery instance provided"
expect(error).toStrictEqual(
new Error("macaroon discharge required but no bakery instance provided")
);
}

Expand Down Expand Up @@ -363,7 +363,9 @@ describe("connect", () => {
},
version: 3,
});
expect(error).toBe("macaroon discharge failed: bad wolf");
expect(error).toStrictEqual(
new Error("macaroon discharge failed: bad wolf")
);
}

it("login discharge required failure", (done) => {
Expand Down

0 comments on commit 0f2871f

Please sign in to comment.