mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-09-05 00:35:21 +00:00
src: Add proper error handling (#283)
Feature: Please see `doc/usage.md` for more information on error-handling.
This commit is contained in:
@@ -36,6 +36,7 @@ exports = module.exports = {
|
||||
* @param {String} path
|
||||
* @param {Object} [options]
|
||||
* @param {String} [options.method=POST] Method to use
|
||||
* @param {Object} [options.update] Update object to send.
|
||||
* @param {Object} [options.message] Message to send. Default to a generic text message
|
||||
* @param {Boolean} [options.https=false] Use https
|
||||
* @return {Promise}
|
||||
@@ -47,6 +48,7 @@ exports = module.exports = {
|
||||
* @param {String} token
|
||||
* @param {Object} [options]
|
||||
* @param {String} [options.method=POST] Method to use
|
||||
* @param {Object} [options.update] Update object to send.
|
||||
* @param {Object} [options.message] Message to send. Default to a generic text message
|
||||
* @param {Boolean} [options.https=false] Use https
|
||||
* @return {Promise}
|
||||
@@ -55,6 +57,9 @@ exports = module.exports = {
|
||||
/**
|
||||
* Start a mock server at the specified port.
|
||||
* @param {Number} port
|
||||
* @param {Object} [options]
|
||||
* @param {Boolean} [options.bad=false] Bad Mock Server; responding with
|
||||
* unparseable messages
|
||||
* @return {Promise}
|
||||
*/
|
||||
startMockServer,
|
||||
@@ -76,10 +81,13 @@ const statics = require('node-static');
|
||||
const servers = {};
|
||||
|
||||
|
||||
function startMockServer(port) {
|
||||
function startMockServer(port, options = {}) {
|
||||
assert.ok(port);
|
||||
const server = http.Server((req, res) => {
|
||||
servers[port].polling = true;
|
||||
if (options.bad) {
|
||||
return res.end('can not be parsed with JSON.parse()');
|
||||
}
|
||||
return res.end(JSON.stringify({
|
||||
ok: true,
|
||||
result: [{
|
||||
@@ -153,11 +161,11 @@ function sendWebHookRequest(port, path, options = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: options.method || 'POST',
|
||||
body: {
|
||||
body: options.update || {
|
||||
update_id: 1,
|
||||
message: options.message || { text: 'test' }
|
||||
},
|
||||
json: options.json || true,
|
||||
json: (typeof options.json === 'undefined') ? true : options.json,
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user