Skip to content

Commit

Permalink
feat: log proxy events
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 19, 2018
1 parent 7611b5d commit ad7b08b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
EVENT_MESSAGE_RECIEVED,
EVENT_MESSAGE_NOT_SENT,
DEFAULT_TIMEOUT_CALLBACK_MESSAGE,
EVENT_MESSAGE_PROXIED,
EVENT_MESSAGE_PROXY_ERROR,
} from './constants'

const DEFAULT_SESSIONS_LIMIT: number = 1000
Expand Down Expand Up @@ -259,7 +261,6 @@ export default class Alice {
])
.then((result) => result)
.catch(async (error) => {
console.log(error)
eventEmitter.dispatch(EVENT_MESSAGE_NOT_SENT)
this.timeoutCallback(new Context({ req, sendResponse }))
})
Expand Down Expand Up @@ -353,9 +354,10 @@ export default class Alice {
body: JSON.stringify(request),
})
const json = await res.json()
eventEmitter.dispatch(EVENT_MESSAGE_PROXIED, { data: request })
return sendResponse(json)
} catch (error) {
console.error(error)
eventEmitter.dispatch(EVENT_MESSAGE_PROXY_ERROR, { data: request })
}
}
}
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const ALICE_API_URL = 'https://dialogs.yandex.net/api/v1/skills'
export const EVENT_MESSAGE_RECIEVED = 'messageRecieved'
export const EVENT_MESSAGE_SENT = 'messageSent'
export const EVENT_MESSAGE_NOT_SENT = 'messageNotSent'
export const EVENT_MESSAGE_PROXIED = 'messageProxied'
export const EVENT_MESSAGE_PROXY_ERROR = 'messageProxyError'

export const DEFAULT_TIMEOUT_CALLBACK_MESSAGE = 'Извините, но я не успела найти ответ за отведенное время.'

module.exports.ALICE_PROTOCOL_VERSION = '1.0'
Expand Down
6 changes: 6 additions & 0 deletions src/middlewares/loggingMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
EVENT_MESSAGE_RECIEVED,
EVENT_MESSAGE_SENT,
EVENT_MESSAGE_NOT_SENT,
EVENT_MESSAGE_PROXIED,
EVENT_MESSAGE_PROXY_ERROR,
} from '../constants'

import chalk from 'chalk'
Expand Down Expand Up @@ -46,6 +48,8 @@ export default function createLoggingMiddleware(opts: ILogger = {}) {
[EVENT_MESSAGE_RECIEVED]: 'info',
[EVENT_MESSAGE_SENT]: 'info',
[EVENT_MESSAGE_NOT_SENT]: 'warn',
[EVENT_MESSAGE_PROXIED]: 'info',
[EVENT_MESSAGE_PROXY_ERROR]: 'error',
}
let isInitialized = false
return (ctx) => {
Expand All @@ -58,6 +62,8 @@ export default function createLoggingMiddleware(opts: ILogger = {}) {
ctx.eventEmitter.subscribe(EVENT_MESSAGE_RECIEVED, logEvent)
ctx.eventEmitter.subscribe(EVENT_MESSAGE_SENT, logEvent)
ctx.eventEmitter.subscribe(EVENT_MESSAGE_NOT_SENT, logEvent)
ctx.eventEmitter.subscribe(EVENT_MESSAGE_PROXIED, logEvent)
ctx.eventEmitter.subscribe(EVENT_MESSAGE_PROXY_ERROR, logEvent)

function logEvent(event) {
try {
Expand Down

0 comments on commit ad7b08b

Please sign in to comment.