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

Fix #248 by adding a new option deferInitialization #1303

Merged
merged 8 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
11 changes: 9 additions & 2 deletions examples/custom-properties/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const app = new App({
},
unhandledRequestTimeoutMillis: 2000, // the default is 3001
}),
// This option enables developer to call #init() in async/await style
deferInitialization: true,
});

app.use(async ({ logger, context, next }) => {
Expand All @@ -41,7 +43,12 @@ app.use(async ({ logger, context, next }) => {

(async () => {
// Start your app
await app.start(process.env.PORT || 3000);

try {
await app.init();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example error pattern:

> [email protected] start /Users/ksera/github/bolt-js/examples/custom-properties
> node http.js

AppInitializationError: Apps used in a single workspace can be initialized with a token. Apps used in many workspaces should be initialized with oauth installer options or authorize.

Since you have not provided a token or authorize, you might be missing one or more required oauth installer options. See https://slack.dev/bolt-js/concepts#authenticating-oauth for these required fields.

    at App.initAuthorizeIfNoTokenIsGiven (/Users/ksera/github/bolt-js/dist/App.js:660:19)
    at App.init (/Users/ksera/github/bolt-js/dist/App.js:180:47)
    at /Users/ksera/github/bolt-js/examples/custom-properties/http.js:46:15
    at Object.<anonymous> (/Users/ksera/github/bolt-js/examples/custom-properties/http.js:53:3)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'slack_bolt_app_initialization_error'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 255
npm ERR! [email protected] start: `node http.js`
npm ERR! Exit status 255

await app.start(process.env.PORT || 3000);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't call init() in advance, this start method fails this way:

$ npm start

> [email protected] start /Users/ksera/github/bolt-js/examples/custom-properties
> node http.js

AppInitializationError: This App instance is not yet initialized. Call `await App#init()` before starting the app.
    at App.start (/Users/ksera/github/bolt-js/dist/App.js:239:19)
    at /Users/ksera/github/bolt-js/examples/custom-properties/http.js:46:15
    at Object.<anonymous> (/Users/ksera/github/bolt-js/examples/custom-properties/http.js:52:3)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'slack_bolt_app_initialization_error'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 255
npm ERR! [email protected] start: `node http.js`
npm ERR! Exit status 255
npm ERR!

} catch (e) {
console.error(e);
process.exit(255);
}
console.log('⚡️ Bolt app is running!');
})();
4 changes: 2 additions & 2 deletions examples/custom-properties/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "bolt-js-custom-properties-app",
"version": "1.0.0",
"description": "Having custom request properties in ⚡️ Bolt for JavaScript",
"main": "app.js",
"main": "http.js",
"scripts": {
"ngrok": "ngrok http 3000",
"start": "node app.js",
"start": "node http.js",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been incorrect since I've renamed the source file.

"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "MIT",
Expand Down
43 changes: 43 additions & 0 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,49 @@ describe('App', () => {
// Assert
assert.instanceOf(app, MockApp);
});

it('should fail in await App#init()', async () => {
// Arrange
const fakeConstructor = sinon.fake();
const overrides = mergeOverrides(withNoopAppMetadata(), {
'@slack/web-api': {
WebClient: class {
public constructor() {
fakeConstructor(...arguments); // eslint-disable-line prefer-rest-params
}

public auth = {
test: () => {
throw new Error('Failing for init() test!');
},
};
},
},
});

const MockApp = await importApp(overrides);
const app = new MockApp({
token: 'xoxb-completely-invalid-token',
signingSecret: 'invalid-one',
deferInitialization: true,
});
// Assert
assert.instanceOf(app, MockApp);
try {
// call #start() before #init()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this test pattern

await app.start();
assert.fail('The start() method should fail before init() call');
} catch (err: any) {
assert.equal(err.message, 'This App instance is not yet initialized. Call `await App#init()` before starting the app.');
}
try {
await app.init();
assert.fail('The init() method should fail here');
} catch (err: any) {
assert.equal(err.message, 'Failing for init() test!');
}
});

// TODO: tests for ignoreSelf option
// TODO: tests for logger and logLevel option
// TODO: tests for providing botId and botUserId options
Expand Down
Loading