Sfoglia il codice sorgente

Refactor RCS number service to handle service unavailable exceptions

x1ongzhu 1 anno fa
parent
commit
498c80d71c
1 ha cambiato i file con 7 aggiunte e 7 eliminazioni
  1. 7 7
      src/rcs-number/rcs-number.service.ts

+ 7 - 7
src/rcs-number/rcs-number.service.ts

@@ -1,5 +1,5 @@
 import { mwze167 } from './impl/mwze167.service'
-import { forwardRef, Inject, Injectable } from '@nestjs/common'
+import { forwardRef, Inject, Injectable, ServiceUnavailableException } from '@nestjs/common'
 import { InjectRepository } from '@nestjs/typeorm'
 import { RcsNumber, RcsNumberSource, RcsNumberStatus } from './entities/rcs-number.entity'
 import { PageRequest } from '../common/dto/page-request'
@@ -73,10 +73,10 @@ export class RcsNumberService {
         if (country) {
             operatorConfig = await this.operatorConfigService.findByCountry(country)
             if (!operatorConfig) {
-                throw new Error(`该国家不可用: ${country}`)
+                throw new ServiceUnavailableException(`该国家不可用: ${country}`)
             }
             if (operatorConfig.matchers.filter((matcher) => matcher.enabled).length === 0) {
-                throw new Error(`该国家无可用运营商: ${country}`)
+                throw new ServiceUnavailableException(`该国家无可用运营商: ${country}`)
             }
         }
         if (!operatorConfig) {
@@ -88,7 +88,7 @@ export class RcsNumberService {
                 (config) => config.matchers.filter((matcher) => matcher.enabled).length > 0
             )
             if (availableOperatorConfigs.length === 0) {
-                throw new Error(`无可用国家`)
+                throw new ServiceUnavailableException(`无可用国家`)
             }
 
             operatorConfig = availableOperatorConfigs[Math.floor(Math.random() * availableOperatorConfigs.length)]
@@ -114,7 +114,7 @@ export class RcsNumberService {
             )
         })
         if (!availableChannels.length) {
-            throw new Error('无可用短信通道')
+            throw new ServiceUnavailableException('无可用短信通道')
         }
         const channel = availableChannels[Math.floor(Math.random() * availableChannels.length)]
         let numberService: GetNumberService
@@ -145,14 +145,14 @@ export class RcsNumberService {
                 numberService = this.xyz
                 break
             default:
-                throw new Error('不支持的短信通道')
+                throw new ServiceUnavailableException('不支持的短信通道')
         }
         const res = await numberService.getNumber(operatorConfig.country)
         const rawNumber = res.number
         res.number = checkAndFormatNumber(operatorConfig.country, res.number, res.operatorName)
         const mapTo = matchOperator(res.operatorCode, operatorConfig)
         if (!mapTo) {
-            throw new Error(
+            throw new ServiceUnavailableException(
                 `无匹配的运营商, country: ${operatorConfig.country}, operatorCode: ${res.operatorCode}, number: ${res.number}`
             )
         }