|
1 | 1 | import { ReportModuleInterface, errorType } from "../../../interfaces/DeepCodeInterfaces"; |
2 | | -import BaseDeepCodeModule from "./BaseDeepCodeModule"; |
3 | | -import { statusCodes } from "../../constants/statusCodes"; |
| 2 | +import BaseDeepCodeModule from './BaseDeepCodeModule'; |
4 | 3 | import { errorsLogs } from "../../messages/errorsServerLogMessages"; |
5 | 4 | import { TELEMETRY_EVENTS } from "../../constants/telemetry"; |
6 | 5 | import { DEEPCODE_CONTEXT, DEEPCODE_ERROR_CODES } from "../../constants/views"; |
7 | 6 | import { MAX_CONNECTION_RETRIES, CONNECTION_ERROR_RETRY_INTERVAL } from "../../constants/general"; |
8 | 7 |
|
9 | | -import { reportEvent, reportError } from '@deepcode/tsc'; |
| 8 | +import { reportEvent, reportError, constants } from '@deepcode/tsc'; |
10 | 9 |
|
11 | 10 | abstract class ReportModule extends BaseDeepCodeModule implements ReportModuleInterface { |
12 | 11 | private transientErrors = 0; |
@@ -84,45 +83,43 @@ abstract class ReportModule extends BaseDeepCodeModule implements ReportModuleIn |
84 | 83 | private async processErrorInternal(error: errorType, options: { [key: string]: any } = {}): Promise<void> { |
85 | 84 | // console.error(`DeepCode error handler: ${JSON.stringify(error)}`); |
86 | 85 |
|
87 | | - if (error.error) { |
88 | | - const { code, message } = error.error; |
89 | | - // TODO: move it to 'tsc' |
90 | | - if (code === 'ENOTFOUND' && message === 'getaddrinfo ENOTFOUND www.deepcode.ai') { |
91 | | - return this.connectionErrorHandler(); |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - if (error.errno) { |
96 | | - if (error.errno === 'ECONNREFUSED') return this.connectionErrorHandler(); |
| 86 | + const defaultErrorHandler = async () => { |
97 | 87 | await this.sendErrorToServer(error, options); |
98 | | - return this.generalErrorHandler(); |
99 | | - } |
| 88 | + await this.generalErrorHandler(); |
| 89 | + }; |
100 | 90 |
|
101 | | - const { |
102 | | - unauthorizedUser, |
103 | | - unauthorizedContent, |
104 | | - unauthorizedBundleAccess, |
105 | | - notFound, |
106 | | - serverError, |
107 | | - badGateway, |
108 | | - serviceUnavailable, |
109 | | - timeout, |
110 | | - } = statusCodes; |
111 | | - |
112 | | - switch (error.statusCode) { |
113 | | - case serverError: |
114 | | - case badGateway: |
115 | | - case serviceUnavailable: |
116 | | - case timeout: |
| 91 | + const errorHandlers: { [P in constants.ErrorCodes]: () => Promise<void> } = { |
| 92 | + [constants.ErrorCodes.serverError]: defaultErrorHandler, |
| 93 | + [constants.ErrorCodes.badGateway]: async () => { |
| 94 | + return this.connectionErrorHandler(); |
| 95 | + }, |
| 96 | + [constants.ErrorCodes.serviceUnavailable]: async () => { |
| 97 | + return this.connectionErrorHandler(); |
| 98 | + }, |
| 99 | + [constants.ErrorCodes.timeout]: async () => { |
| 100 | + return this.connectionErrorHandler(); |
| 101 | + }, |
| 102 | + [constants.ErrorCodes.dnsNotFound]: async () => { |
| 103 | + return this.connectionErrorHandler(); |
| 104 | + }, |
| 105 | + [constants.ErrorCodes.connectionRefused]: async () => { |
117 | 106 | return this.connectionErrorHandler(); |
118 | | - case unauthorizedContent: |
119 | | - case unauthorizedBundleAccess: |
120 | | - case unauthorizedUser: |
| 107 | + }, |
| 108 | + [constants.ErrorCodes.loginInProgress]: async () => {}, |
| 109 | + [constants.ErrorCodes.unauthorizedContent]: async () => {}, |
| 110 | + [constants.ErrorCodes.unauthorizedUser]: async () => { |
121 | 111 | return this.authenticationErrorHandler(); |
122 | | - case notFound: |
123 | | - default: |
124 | | - await this.sendErrorToServer(error, options); |
125 | | - return this.generalErrorHandler(); |
| 112 | + }, |
| 113 | + [constants.ErrorCodes.unauthorizedBundleAccess]: async () => {}, |
| 114 | + [constants.ErrorCodes.notFound]: async () => {}, |
| 115 | + [constants.ErrorCodes.bigPayload]: async () => {}, |
| 116 | + }; |
| 117 | + |
| 118 | + const errorHandler = errorHandlers[error.statusCode]; |
| 119 | + if (errorHandler) { |
| 120 | + await errorHandler(); |
| 121 | + } else { |
| 122 | + await defaultErrorHandler(); |
126 | 123 | } |
127 | 124 | } |
128 | 125 |
|
|
0 commit comments