@@ -11,6 +11,7 @@ export const ERRORS = {
1111 CERTIFICATE_EXPIRES_SOON : 'CERTIFICATE_EXPIRES_SOON' ,
1212 CERTIFICATE_IS_NOT_VALIDATED : 'CERTIFICATE_IS_NOT_VALIDATED' ,
1313 CERTIFICATE_IS_NOT_VALID : 'CERTIFICATE_IS_NOT_VALID' ,
14+ ZERO_SSL_API_ERROR : 'ZERO_SSL_API_ERROR' ,
1415} ;
1516
1617/**
@@ -68,9 +69,22 @@ export default function validateZeroSslCertificateFactory(homeDir, getCertificat
6869 data . isBundleFilePresent = fs . existsSync ( data . bundleFilePath ) ;
6970
7071 // This function will throw an error if certificate with specified ID is not present
71- const certificate = await getCertificate ( data . apiKey , certificateId ) ;
72+ try {
73+ data . certificate = await getCertificate ( data . apiKey , certificateId ) ;
74+ } catch ( e ) {
75+ if ( e . code ) {
76+ data . error = e ;
7277
73- data . isExpiresSoon = certificate . isExpiredInDays ( expirationDays ) ;
78+ return {
79+ error : ERRORS . ZERO_SSL_API_ERROR ,
80+ data,
81+ } ;
82+ }
83+
84+ throw e ;
85+ }
86+
87+ data . isExpiresSoon = data . certificate . isExpiredInDays ( expirationDays ) ;
7488
7589 // If certificate exists but private key does not, then we can't setup TLS connection
7690 // In this case we need to regenerate a certificate or put back this private key
@@ -82,17 +96,16 @@ export default function validateZeroSslCertificateFactory(homeDir, getCertificat
8296 }
8397
8498 // We need to make sure that external IP and certificate IP match
85- if ( certificate . common_name !== data . externalIp ) {
99+ if ( data . certificate . common_name !== data . externalIp ) {
86100 return {
87101 error : ERRORS . EXTERNAL_IP_MISMATCH ,
88102 data,
89103 } ;
90104 }
91105
92- if ( [ 'pending_validation' , 'draft' ] . includes ( certificate . status ) ) {
106+ if ( [ 'pending_validation' , 'draft' ] . includes ( data . certificate . status ) ) {
93107 // Certificate is already created, so we just need to pass validation
94108 // and download certificate file
95- data . certificate = certificate ;
96109
97110 // We need to download new certificate bundle
98111 data . isBundleFilePresent = false ;
@@ -103,7 +116,7 @@ export default function validateZeroSslCertificateFactory(homeDir, getCertificat
103116 } ;
104117 }
105118
106- if ( certificate . status !== 'issued' || data . isExpiresSoon ) {
119+ if ( data . certificate . status !== 'issued' || data . isExpiresSoon ) {
107120 // Certificate is going to expire soon, or current certificate is not valid
108121 // we need to obtain a new one
109122
@@ -128,8 +141,6 @@ export default function validateZeroSslCertificateFactory(homeDir, getCertificat
128141 }
129142
130143 // Certificate is valid, so we might need only to download certificate bundle
131- data . certificate = certificate ;
132-
133144 return {
134145 data,
135146 } ;
0 commit comments