@@ -18,7 +18,11 @@ import {
1818} from '../../common/rest_types' ;
1919import { AppContextService } from './app_context' ;
2020import { AutoOpsConfig } from '../types' ;
21+ import { AutoOpsError } from './errors' ;
2122
23+ const AGENT_CREATION_FAILED_ERROR = 'AutoOps API could not create the autoops agent' ;
24+ const AUTO_OPS_AGENT_CREATION_PREFIX = '[AutoOps API] Creating autoops agent failed' ;
25+ const AUTO_OPS_MISSING_CONFIG_ERROR = 'Missing autoops configuration' ;
2226export class AutoOpsAPIService {
2327 constructor ( private appContextService : AppContextService ) { }
2428 public async autoOpsUsageMetricsAPI ( requestBody : UsageMetricsRequestBody ) {
@@ -34,8 +38,8 @@ export class AutoOpsAPIService {
3438
3539 const autoopsConfig = this . appContextService . getConfig ( ) ?. autoops ;
3640 if ( ! autoopsConfig ) {
37- logger . error ( ' [AutoOps API] Missing autoops configuration' , errorMetadata ) ;
38- throw new Error ( 'missing autoops configuration' ) ;
41+ logger . error ( ` [AutoOps API] ${ AUTO_OPS_MISSING_CONFIG_ERROR } ` , errorMetadata ) ;
42+ throw new AutoOpsError ( AUTO_OPS_MISSING_CONFIG_ERROR ) ;
3943 }
4044
4145 logger . debug (
@@ -86,7 +90,7 @@ export class AutoOpsAPIService {
8690 ( error : Error | AxiosError ) => {
8791 if ( ! axios . isAxiosError ( error ) ) {
8892 logger . error (
89- `[AutoOps API] Creating autoops failed with an error ${ error } ${ requestConfigDebugStatus } ` ,
93+ `${ AUTO_OPS_AGENT_CREATION_PREFIX } with an error ${ error } ${ requestConfigDebugStatus } ` ,
9094 errorMetadataWithRequestConfig
9195 ) ;
9296 throw new Error ( withRequestIdMessage ( error . message ) ) ;
@@ -97,7 +101,7 @@ export class AutoOpsAPIService {
97101 if ( error . response ) {
98102 // The request was made and the server responded with a status code and error data
99103 logger . error (
100- `[AutoOps API] Creating autoops failed because the AutoOps API responding with a status code that falls out of the range of 2xx: ${ JSON . stringify (
104+ `${ AUTO_OPS_AGENT_CREATION_PREFIX } because the AutoOps API responded with a status code that falls out of the range of 2xx: ${ JSON . stringify (
101105 error . response . status
102106 ) } } ${ JSON . stringify ( error . response . data ) } } ${ requestConfigDebugStatus } `,
103107 {
@@ -111,30 +115,26 @@ export class AutoOpsAPIService {
111115 } ,
112116 }
113117 ) ;
114- throw new Error (
115- withRequestIdMessage ( `the AutoOps API could not create the autoops agent` )
116- ) ;
118+ throw new AutoOpsError ( withRequestIdMessage ( AGENT_CREATION_FAILED_ERROR ) ) ;
117119 } else if ( error . request ) {
118120 // The request was made but no response was received
119121 logger . error (
120- `[AutoOps API] Creating autoops agent failed while sending the request to the AutoOps API: ${ errorLogCodeCause } ${ requestConfigDebugStatus } ` ,
122+ `${ AUTO_OPS_AGENT_CREATION_PREFIX } while sending the request to the AutoOps API: ${ errorLogCodeCause } ${ requestConfigDebugStatus } ` ,
121123 errorMetadataWithRequestConfig
122124 ) ;
123125 throw new Error ( withRequestIdMessage ( `no response received from the AutoOps API` ) ) ;
124126 } else {
125127 // Something happened in setting up the request that triggered an Error
126128 logger . error (
127- `[AutoOps API] Creating autoops agent failed to be created ${ errorLogCodeCause } ${ requestConfigDebugStatus } ` ,
129+ `${ AUTO_OPS_AGENT_CREATION_PREFIX } to be created ${ errorLogCodeCause } ${ requestConfigDebugStatus } ` ,
128130 errorMetadataWithRequestConfig
129131 ) ;
130- throw new Error (
131- withRequestIdMessage ( 'the AutoOps API could not create the autoops agent' )
132- ) ;
132+ throw new AutoOpsError ( withRequestIdMessage ( AGENT_CREATION_FAILED_ERROR ) ) ;
133133 }
134134 }
135135 ) ;
136136
137- logger . debug ( `[AutoOps API] Created an autoops agent ${ response } ` ) ;
137+ logger . debug ( `[AutoOps API] Successfully created an autoops agent ${ response } ` ) ;
138138 return response ;
139139 }
140140
0 commit comments