Skip to content

Commit

Permalink
feat(API): Added error handling for the event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Mar 15, 2022
1 parent 91c7c89 commit 2876b1c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/helpers/ErrorsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum Errors {
CLASS_INITIALIZATION_ERROR = "InitializationError",
INVALID_TYPE = "Invalid Type",
MISSING_PARAMS = "Missing Parameters",
INVALID_EVENT = "Invalid Event",
}

export class ErrosController extends Error {
Expand Down
66 changes: 65 additions & 1 deletion src/structure/TelegramAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ export class TelegramAPI {

if (!res.ok) {
this.emitter.emit("error", res);
return;
}

return res;
Expand All @@ -192,6 +191,11 @@ export class TelegramAPI {
event: E,
listener: (...callbacks: TelegramEvents[E]) => void
) {
if (!event)
throw new ErrosController(
"You should provide a valid event",
Errors.INVALID_EVENT
);
//@ts-expect-error
this.emitter.on(event, listener);
}
Expand All @@ -207,51 +211,111 @@ export class TelegramAPI {
event: E,
listener: (...callbacks: TelegramEvents[E]) => void
) {
if (!event)
throw new ErrosController(
"You should provide a valid event",
Errors.INVALID_EVENT
);
//@ts-expect-error
this.emitter.once(event, listener);
}

onMessage(callback: TMessageCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onMessageCallback = callback;
}

onEditedMessage(callback: TMessageCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onEditedMessageCallback = callback;
}

onChannelPost(callback: TMessageCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onChannelPostCallback = callback;
}

onEditedChannelPost(callback: TMessageCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onEditedChannelPostCallback = callback;
}

onCallbackQuery(callback: TCallbackQueryCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onCallbackQueryCallback = callback;
}

onInlineQuery(callback: TInlineQueryCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onInlineQueryCallback = callback;
}

onChosenInlineResult(callback: TChosenInlineResultCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onChosenInlineResultCallback = callback;
}

onShippingQuery(callback: TShippingQueryCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onShippingQueryCallback = callback;
}

onPreCheckoutQuery(callback: TPreCheckoutQueryCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onPreCheckoutQueryCallback = callback;
}

onPoll(callback: TPollCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onPollCallback = callback;
}

onPollAnswer(callback: TPollAnswerCallback) {
if (typeof callback !== "function")
throw new ErrosController(
`The paramater callback must be from type 'function', recieved: ${typeof callback}`,
Errors.INVALID_TYPE
);
this.onPollAnswerCallback = callback;
}

Expand Down

0 comments on commit 2876b1c

Please sign in to comment.