minor modifications

This commit is contained in:
atharva.dev 2025-10-22 12:00:14 +05:30
parent aabc1e9af8
commit 07c6b03e04
1 changed files with 35 additions and 91 deletions

View File

@ -13,6 +13,34 @@ export class SecloreDRMApiService {
this.baseUrl = baseUrl;
}
/**
* Common error handler for HTTP responses
* @param error - The error object from httpRequest
* @param customMessages - Optional custom error messages for specific status codes
*/
private handleHttpError(error: any, customMessages?: { [statusCode: number]: string }): never {
const statusCode = error.statusCode;
const errorResponse = error.response?.body as IErrorResponse;
if (customMessages && customMessages[statusCode]) {
throw new Error(`${customMessages[statusCode]}: ${errorResponse?.errorMessage || 'Unknown error'}`);
}
// Default error handling
switch (statusCode) {
case 400:
throw new Error(`Bad Request: ${errorResponse?.errorMessage || 'Invalid request data'}`);
case 401:
throw new Error(`Unauthorized: ${errorResponse?.errorMessage || 'Invalid credentials'}`);
case 413:
throw new Error(`Payload Too Large: ${errorResponse?.errorMessage || 'File size exceeds limit'}`);
case 500:
throw new Error(`Server Error: ${errorResponse?.errorMessage || 'Internal server error'}`);
default:
throw error;
}
}
/**
* Login Endpoint to generate Access Token and Refresh Token for JWT Authorization.
* Upon successful login, all the existing previous tokens for that tenant will be invalidated.
@ -50,16 +78,7 @@ export class SecloreDRMApiService {
const response = await this.context.helpers.httpRequest(options);
return response as ILoginResponse;
} catch (error: any) {
// Handle specific HTTP error responses
if (error.statusCode === 401) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Unauthorized: ${errorResponse?.errorMessage || 'Invalid credentials'}`);
} else if (error.statusCode === 500) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Server Error: ${errorResponse?.errorMessage || 'Internal server error'}`);
}
// Re-throw other errors
throw error;
this.handleHttpError(error);
}
}
@ -98,17 +117,7 @@ export class SecloreDRMApiService {
const response = await this.context.helpers.httpRequest(options);
return response as ILoginResponse;
} catch (error: any) {
// Handle specific HTTP error responses
if (error.statusCode === 401) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Unauthorized: ${errorResponse?.errorMessage || 'Invalid refresh token'}`);
} else if (error.statusCode === 500) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Server Error: ${errorResponse?.errorMessage || 'Internal server error'}`);
}
// Re-throw other errors
throw error;
this.handleHttpError(error, { 401: 'Unauthorized' });
}
}
@ -148,20 +157,7 @@ export class SecloreDRMApiService {
const response = await this.context.helpers.httpRequest(options);
return response as IProtectWithExternalRefIdResponse;
} catch (error: any) {
// Handle specific HTTP error responses
if (error.statusCode === 400) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Bad Request: ${errorResponse?.errorMessage || 'Invalid request data'}`);
} else if (error.statusCode === 401) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Unauthorized: ${errorResponse?.errorMessage || 'Invalid access token'}`);
} else if (error.statusCode === 500) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Server Error: ${errorResponse?.errorMessage || 'Internal server error'}`);
}
// Re-throw other errors
throw error;
this.handleHttpError(error);
}
}
@ -201,20 +197,7 @@ export class SecloreDRMApiService {
const response = await this.context.helpers.httpRequest(options);
return response as IProtectWithFileIdResponse;
} catch (error: any) {
// Handle specific HTTP error responses
if (error.statusCode === 400) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Bad Request: ${errorResponse?.errorMessage || 'Invalid request data'}`);
} else if (error.statusCode === 401) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Unauthorized: ${errorResponse?.errorMessage || 'Invalid access token'}`);
} else if (error.statusCode === 500) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Server Error: ${errorResponse?.errorMessage || 'Internal server error'}`);
}
// Re-throw other errors
throw error;
this.handleHttpError(error);
}
}
@ -254,20 +237,7 @@ export class SecloreDRMApiService {
const response = await this.context.helpers.httpRequest(options);
return response as IProtectWithHotFolderResponse;
} catch (error: any) {
// Handle specific HTTP error responses
if (error.statusCode === 400) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Bad Request: ${errorResponse?.errorMessage || 'Invalid request data'}`);
} else if (error.statusCode === 401) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Unauthorized: ${errorResponse?.errorMessage || 'Invalid access token'}`);
} else if (error.statusCode === 500) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Server Error: ${errorResponse?.errorMessage || 'Internal server error'}`);
}
// Re-throw other errors
throw error;
this.handleHttpError(error);
}
}
@ -307,20 +277,7 @@ export class SecloreDRMApiService {
const response = await this.context.helpers.httpRequest(options);
return response as IUnprotectResponse;
} catch (error: any) {
// Handle specific HTTP error responses
if (error.statusCode === 400) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Bad Request: ${errorResponse?.errorMessage || 'Invalid request data'}`);
} else if (error.statusCode === 401) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Unauthorized: ${errorResponse?.errorMessage || 'Invalid access token'}`);
} else if (error.statusCode === 500) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Server Error: ${errorResponse?.errorMessage || 'Internal server error'}`);
}
// Re-throw other errors
throw error;
this.handleHttpError(error);
}
}
@ -364,20 +321,7 @@ export class SecloreDRMApiService {
const response = await this.context.helpers.httpRequest(options);
return response as IFileUploadResponse;
} catch (error: any) {
// Handle specific HTTP error responses
if (error.statusCode === 401) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Unauthorized: ${errorResponse?.errorMessage || 'Invalid access token'}`);
} else if (error.statusCode === 413) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Payload Too Large: ${errorResponse?.errorMessage || 'File size exceeds limit'}`);
} else if (error.statusCode === 500) {
const errorResponse = error.response?.body as IErrorResponse;
throw new Error(`Server Error: ${errorResponse?.errorMessage || 'Internal server error'}`);
}
// Re-throw other errors
throw error;
this.handleHttpError(error);
}
}