<commit_before>import Router, { RouterContext } from '@koa/router'
import httpErrors from 'http-errors'
import Joi from 'joi'
import Koa from 'koa'
import { container } from 'tsyringe'
import { assertUnreachable } from '../../../common/assert-unreachable'
import { SendChatMessageServerBody } from '../../../common/chat'
import { CHANNEL_MAXLENGTH, CHANNEL_PATTERN } from '../../../common/constants'
import { MULTI_CHANNEL } from '../../../common/flags'
import { featureEnabled } from '../flags/feature-enabled'
import { httpApi, HttpApi } from '../http/http-api'
import ensureLoggedIn from '../session/ensure-logged-in'
import createThrottle from '../throttle/create-throttle'
import throttleMiddleware from '../throttle/middleware'
import { validateRequest } from '../validation/joi-validator'
import ChatService, { ChatServiceError, ChatServiceErrorCode } from './chat-service'

const joinThrottle = createThrottle('chatjoin', {
  rate: 3,
  burst: 10,
  window: 60000,
})

const leaveThrottle = createThrottle('chatleave', {
  rate: 10,
  burst: 20,
  window: 60000,
})

const sendThrottle = createThrottle('chatsend', {
  rate: 30,
  burst: 90,
  window: 60000,
})

const retrievalThrottle = createThrottle('chatretrieval', {
  rate: 30,
  burst: 120,
  window: 60000,
})

function isChatServiceError(error: Error): error is ChatServiceError {
  return error.hasOwnProperty('code')
}

function convertChatServiceError(err: Error) {
  if (!isChatServiceError(err)) {
    throw err
  }

  switch (err.code) {
    case ChatServiceErrorCode.UserOffline:
      throw new httpErrors.NotFound(err.message)
    case ChatServiceErrorCode.InvalidJoinAction:
    case ChatServiceErrorCode.InvalidLeaveAction:
    case ChatServiceErrorCode.InvalidSendAction:
    case ChatServiceErrorCode.InvalidGetHistoryAction:
    case ChatServiceErrorCode.InvalidGetUsersAction:
      throw new httpErrors.BadRequest(err.message)
    case ChatServiceErrorCode.LeaveShieldBattery:
      throw new httpErrors.Forbidden(err.message)
    default:
      assertUnreachable(err.code)
  }<commit_msg>Check if things are ChatServiceErrors more selectively so we don't swallow stacks for DB errors.<commit_after>import Router, { RouterContext } from '@koa/router'
import httpErrors from 'http-errors'
import Joi from 'joi'
import Koa from 'koa'
import { container } from 'tsyringe'
import { assertUnreachable } from '../../../common/assert-unreachable'
import { SendChatMessageServerBody } from '../../../common/chat'
import { CHANNEL_MAXLENGTH, CHANNEL_PATTERN } from '../../../common/constants'
import { MULTI_CHANNEL } from '../../../common/flags'
import { featureEnabled } from '../flags/feature-enabled'
import { httpApi, HttpApi } from '../http/http-api'
import ensureLoggedIn from '../session/ensure-logged-in'
import createThrottle from '../throttle/create-throttle'
import throttleMiddleware from '../throttle/middleware'
import { validateRequest } from '../validation/joi-validator'
import ChatService, { ChatServiceError, ChatServiceErrorCode } from './chat-service'

const joinThrottle = createThrottle('chatjoin', {
  rate: 3,
  burst: 10,
  window: 60000,
})

const leaveThrottle = createThrottle('chatleave', {
  rate: 10,
  burst: 20,
  window: 60000,
})

const sendThrottle = createThrottle('chatsend', {
  rate: 30,
  burst: 90,
  window: 60000,
})

const retrievalThrottle = createThrottle('chatretrieval', {
  rate: 30,
  burst: 120,
  window: 60000,
})

function convertChatServiceError(err: Error) {
  if (!(err instanceof ChatServiceError)) {
    throw err
  }

  switch (err.code) {
    case ChatServiceErrorCode.UserOffline:
      throw new httpErrors.NotFound(err.message)
    case ChatServiceErrorCode.InvalidJoinAction:
    case ChatServiceErrorCode.InvalidLeaveAction:
    case ChatServiceErrorCode.InvalidSendAction:
    case ChatServiceErrorCode.InvalidGetHistoryAction:
    case ChatServiceErrorCode.InvalidGetUsersAction:
      throw new httpErrors.BadRequest(err.message)
    case ChatServiceErrorCode.LeaveShieldBattery:
      throw new httpErrors.Forbidden(err.message)
    default:
      assertUnreachable(err.code)
  }
{'fraction_non_alphanumeric': 0.11897261933607947, 'fraction_numerical': 0.017446086745820207, 'mean_word_length': 4.431578947368421, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7219882', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1268, 'n_words': 302}