Skip to content

Commit

Permalink
feat: enhanced error message for missing canister id (#759)
Browse files Browse the repository at this point in the history
* feat: enhanced error message for missing canister id

* adding test

* changelog
  • Loading branch information
krpeacock authored Aug 29, 2023
1 parent b7abf4a commit ac76756
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li></li>
<li>
Adds more helpful error message for when principal is undefined during actor creation
</li>
</ul>
<h2>Version 0.19.2</h2>
<ul>
Expand Down
14 changes: 14 additions & 0 deletions packages/agent/src/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CallRequest, SubmitRequestType, UnSigned } from './agent/http/types';
import * as cbor from './cbor';
import { requestIdOf } from './request_id';
import * as pollingImport from './polling';
import { ActorConfig } from './actor';

const importActor = async (mockUpdatePolling?: () => void) => {
jest.dontMock('./polling');
Expand Down Expand Up @@ -325,6 +326,19 @@ describe('makeActor', () => {
);
}
});
it('should throw a helpful error if the canisterId is not set', async () => {
const httpAgent = new HttpAgent({ host: 'http://localhost' });
const actorInterface = () => {
return IDL.Service({
greet: IDL.Func([IDL.Text], [IDL.Text]),
});
};
const { Actor } = await importActor();
const config = { agent: httpAgent } as any as ActorConfig;
expect(() => Actor.createActor(actorInterface, config)).toThrowError(
'Canister ID is required, but recieved undefined instead. If you are using automatically generated declarations, this may be because your application is not setting the canister ID in process.env correctly.',
);
});
});

// TODO: tests for rejected, unknown time out
9 changes: 9 additions & 0 deletions packages/agent/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ export class Actor {
[x: string]: ActorMethod;

constructor(config: ActorConfig) {
if (!config.canisterId)
throw new AgentError(
`Canister ID is required, but recieved ${typeof config.canisterId} instead. If you are using automatically generated declarations, this may be because your application is not setting the canister ID in process.env correctly.`,
);
const canisterId =
typeof config.canisterId === 'string'
? Principal.fromText(config.canisterId)
Expand Down Expand Up @@ -316,6 +320,11 @@ export class Actor {
interfaceFactory: IDL.InterfaceFactory,
configuration: ActorConfig,
): ActorSubclass<T> {
if (!configuration.canisterId) {
throw new AgentError(
`Canister ID is required, but recieved ${typeof configuration.canisterId} instead. If you are using automatically generated declarations, this may be because your application is not setting the canister ID in process.env correctly.`,
);
}
return new (this.createActorClass(interfaceFactory))(
configuration,
) as unknown as ActorSubclass<T>;
Expand Down

0 comments on commit ac76756

Please sign in to comment.