Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhanced error message for missing canister id #759

Merged
merged 4 commits into from
Aug 29, 2023
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
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
Loading