fix: adds try...catch in all services (#2494)

Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
This commit is contained in:
Piyush Gupta
2024-04-22 12:45:37 +05:30
committed by GitHub
parent ed509d3a9a
commit 4ee27f9618
33 changed files with 1686 additions and 1229 deletions

View File

@@ -15,13 +15,17 @@ export const canUserAccessApiKey = async (userId: string, apiKeyId: string): Pro
async () => {
validateInputs([userId, ZId], [apiKeyId, ZId]);
const apiKeyFromServer = await getApiKey(apiKeyId);
if (!apiKeyFromServer) return false;
try {
const apiKeyFromServer = await getApiKey(apiKeyId);
if (!apiKeyFromServer) return false;
const hasAccessToEnvironment = await hasUserEnvironmentAccess(userId, apiKeyFromServer.environmentId);
if (!hasAccessToEnvironment) return false;
const hasAccessToEnvironment = await hasUserEnvironmentAccess(userId, apiKeyFromServer.environmentId);
if (!hasAccessToEnvironment) return false;
return true;
return true;
} catch (error) {
throw error;
}
},
[`canUserAccessApiKey-${userId}-${apiKeyId}`],

View File

@@ -83,7 +83,10 @@ export const getApiKeys = async (environmentId: string, page?: number): Promise<
export const hashApiKey = (key: string): string => createHash("sha256").update(key).digest("hex");
export async function createApiKey(environmentId: string, apiKeyData: TApiKeyCreateInput): Promise<TApiKey> {
export const createApiKey = async (
environmentId: string,
apiKeyData: TApiKeyCreateInput
): Promise<TApiKey> => {
validateInputs([environmentId, ZId], [apiKeyData, ZApiKeyCreateInput]);
try {
const key = randomBytes(16).toString("hex");
@@ -110,7 +113,7 @@ export async function createApiKey(environmentId: string, apiKeyData: TApiKeyCre
}
throw error;
}
}
};
export const getApiKeyFromKey = async (apiKey: string): Promise<TApiKey | null> => {
const hashedKey = getHash(apiKey);