11import errorFormatter from './contentstackError'
2+ import { ERROR_MESSAGES } from './errorMessages'
23
34/**
45 * @description OAuthHandler class to handle OAuth authorization and token management
@@ -91,7 +92,7 @@ export default class OAuthHandler {
9192 async authorize ( ) {
9293 try {
9394 if ( ! this . OAuthBaseURL ) {
94- throw new Error ( 'OAuthBaseURL is not set' )
95+ throw new Error ( ERROR_MESSAGES . OAUTH_BASE_URL_NOT_SET )
9596 }
9697 const baseUrl = `${ this . OAuthBaseURL } /#!/apps/${ this . appId } /authorize`
9798 const authUrl = new URL ( baseUrl )
@@ -171,7 +172,7 @@ export default class OAuthHandler {
171172 const refreshToken = providedRefreshToken || this . axiosInstance . oauth . refreshToken
172173
173174 if ( ! refreshToken ) {
174- throw new Error ( 'No refresh token available. Please authenticate first.' )
175+ throw new Error ( ERROR_MESSAGES . NO_REFRESH_TOKEN )
175176 }
176177
177178 const body = new URLSearchParams ( {
@@ -308,7 +309,7 @@ export default class OAuthHandler {
308309 */
309310 setAccessToken ( token ) {
310311 if ( ! token ) {
311- throw new Error ( 'Access token is required' )
312+ throw new Error ( ERROR_MESSAGES . ACCESS_TOKEN_REQUIRED )
312313 }
313314 this . axiosInstance . oauth . accessToken = token
314315 }
@@ -327,7 +328,7 @@ export default class OAuthHandler {
327328 */
328329 setRefreshToken ( token ) {
329330 if ( ! token ) {
330- throw new Error ( 'Refresh token is required' )
331+ throw new Error ( ERROR_MESSAGES . REFRESH_TOKEN_REQUIRED )
331332 }
332333 this . axiosInstance . oauth . refreshToken = token
333334 }
@@ -346,7 +347,7 @@ export default class OAuthHandler {
346347 */
347348 setOrganizationUID ( organizationUID ) {
348349 if ( ! organizationUID ) {
349- throw new Error ( 'Organization UID is required' )
350+ throw new Error ( ERROR_MESSAGES . ORGANIZATION_UID_REQUIRED )
350351 }
351352 this . axiosInstance . oauth . organizationUID = organizationUID
352353 }
@@ -365,7 +366,7 @@ export default class OAuthHandler {
365366 */
366367 setUserUID ( userUID ) {
367368 if ( ! userUID ) {
368- throw new Error ( 'User UID is required' )
369+ throw new Error ( ERROR_MESSAGES . USER_UID_REQUIRED )
369370 }
370371 this . axiosInstance . oauth . userUID = userUID
371372 }
@@ -384,7 +385,7 @@ export default class OAuthHandler {
384385 */
385386 setTokenExpiryTime ( expiryTime ) {
386387 if ( ! expiryTime ) {
387- throw new Error ( 'Token expiry time is required' )
388+ throw new Error ( ERROR_MESSAGES . TOKEN_EXPIRY_REQUIRED )
388389 }
389390 this . axiosInstance . oauth . tokenExpiryTime = expiryTime
390391 }
@@ -414,7 +415,7 @@ export default class OAuthHandler {
414415 errorFormatter ( error )
415416 }
416417 } else {
417- throw new Error ( 'Authorization code not found in redirect URL.' )
418+ throw new Error ( ERROR_MESSAGES . AUTH_CODE_NOT_FOUND )
418419 }
419420 }
420421
@@ -443,11 +444,11 @@ export default class OAuthHandler {
443444 const userUid = this . axiosInstance . oauth . userUID
444445 const currentUserAuthorization = data ?. data ?. filter ( ( element ) => element . user . uid === userUid ) || [ ]
445446 if ( currentUserAuthorization . length === 0 ) {
446- throw new Error ( 'No authorizations found for current user!' )
447+ throw new Error ( ERROR_MESSAGES . NO_USER_AUTHORIZATIONS )
447448 }
448449 return currentUserAuthorization [ 0 ] . authorization_uid // filter authorizations by current logged in user
449450 } else {
450- throw new Error ( 'No authorizations found for the app!' )
451+ throw new Error ( ERROR_MESSAGES . NO_APP_AUTHORIZATIONS )
451452 }
452453 } catch ( error ) {
453454 errorFormatter ( error )
0 commit comments