Skip to content

Commit

Permalink
Merge pull request #3424 from uselagoon/additional-error-handling
Browse files Browse the repository at this point in the history
chore: handle more duplicate insert errors
  • Loading branch information
tobybellwood authored Jun 19, 2024
2 parents 02d817c + 64bfbea commit 7716346
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 103 deletions.
64 changes: 44 additions & 20 deletions services/api/src/resources/backup/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,28 @@ export const addBackup: ResolverFn = async (
project: environment.project
});

const { insertId } = await query(
sqlClientPool,
Sql.insertBackup({
id,
environment: environmentId,
source,
backupId,
created
})
);
let insertId: number;
try {
({insertId} = await query(
sqlClientPool,
Sql.insertBackup({
id,
environment: environmentId,
source,
backupId,
created
})
));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error adding backup. Backup already exists.`
);
} else {
throw new Error(error.message);
}
};

const rows = await query(sqlClientPool, Sql.selectBackup(insertId));
const backup = R.prop(0, rows);

Expand Down Expand Up @@ -246,16 +258,28 @@ export const addRestore: ResolverFn = async (
project: R.path(['0', 'pid'], perms)
});

const { insertId } = await query(
sqlClientPool,
Sql.insertRestore({
id,
backupId,
status,
restoreLocation,
created
})
);
let insertId: number;
try {
({insertId} = await query(
sqlClientPool,
Sql.insertRestore({
id,
backupId,
status,
restoreLocation,
created
})
));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error adding restore. Restore already exists.`
);
} else {
throw new Error(error.message);
}
};

let rows = await query(sqlClientPool, Sql.selectRestore(insertId));
const restoreData = R.prop(0, rows);

Expand Down
106 changes: 69 additions & 37 deletions services/api/src/resources/fact/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,32 @@ export const processAddFacts = async (facts, sqlClientPool, hasPermission, admin
const returnFacts = [];
for (let i = 0; i < facts.length; i++) {
const { environment, name, value, source, description, type, category, keyFact, service } = facts[i];
const {
insertId
} = await query(
sqlClientPool,
Sql.insertFact({
environment,
name,
value,
source,
description,
type,
keyFact,
category,
service
})
);

let insertId: number;
try {
({insertId} = await query(
sqlClientPool,
Sql.insertFact({
environment,
name,
value,
source,
description,
type,
keyFact,
category,
service
})
));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error adding fact. Fact already exists.`
);
} else {
throw new Error(error.message);
}
};

const rows = await query(sqlClientPool, Sql.selectFactByDatabaseId(insertId));
returnFacts.push(R.prop(0, rows));
Expand All @@ -235,20 +245,31 @@ export const addFact: ResolverFn = async (
project: environment.project
});

const { insertId } = await query(
sqlClientPool,
Sql.insertFact({
environment: environmentId,
name,
value,
source,
description,
type,
keyFact,
category,
service
}),
);
let insertId: number;
try {
({insertId} = await query(
sqlClientPool,
Sql.insertFact({
environment: environmentId,
name,
value,
source,
description,
type,
keyFact,
category,
service
}),
));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error adding fact. Fact already exists.`
);
} else {
throw new Error(error.message);
}
};

const rows = await query(
sqlClientPool,
Expand Down Expand Up @@ -423,13 +444,24 @@ export const addFactReference: ResolverFn = async (
project: environment.project
});

const { insertId } = await query(
sqlClientPool,
Sql.insertFactReference({
fid,
name
})
);
let insertId: number;
try {
({insertId} = await query(
sqlClientPool,
Sql.insertFactReference({
fid,
name
})
));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error adding fact reference. Fact reference already exists.`
);
} else {
throw new Error(error.message);
}
};

const rows = await query(
sqlClientPool,
Expand Down
80 changes: 74 additions & 6 deletions services/api/src/resources/notification/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ const DISABLE_NON_ORGANIZATION_NOTIFICATION_ASSIGNMENT = process.env.DISABLE_NON

const addNotificationGeneric = async (sqlClientPool, notificationTable, input) => {
const createSql = knex(notificationTable).insert(input).toString();
let { insertId } = await query(sqlClientPool, createSql);

let insertId: number;
try {
({insertId} = await query(sqlClientPool, createSql));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error adding notification ${input.name}. Notification already exists`
)
} else {
throw new Error(error.message);
}
};

return await query(sqlClientPool, knex(notificationTable).where('id', insertId).toString());
}

Expand Down Expand Up @@ -508,7 +521,18 @@ export const updateNotificationMicrosoftTeams: ResolverFn = async (

await checkNotificationUpdatePermissions(check, hasPermission)

await query(sqlClientPool, Sql.updateNotificationMicrosoftTeams(input));
try {
await query(sqlClientPool, Sql.updateNotificationMicrosoftTeams(input));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error renaming notification ${input.name}. Notification ${input.patch.name} already exists`
)
} else {
throw new Error(error.message);
}
};

const rows = await query(
sqlClientPool,
Sql.selectNotificationMicrosoftTeamsByName(name)
Expand Down Expand Up @@ -538,7 +562,18 @@ export const updateNotificationWebhook: ResolverFn = async (

await checkNotificationUpdatePermissions(check, hasPermission)

await query(sqlClientPool, Sql.updateNotificationWebhook(input));
try {
await query(sqlClientPool, Sql.updateNotificationMicrosoftTeams(input));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error renaming notification ${input.name}. Notification ${input.patch.name} already exists`
)
} else {
throw new Error(error.message);
}
};

const rows = await query(
sqlClientPool,
Sql.selectNotificationWebhookByName(name),
Expand All @@ -565,7 +600,18 @@ export const updateNotificationEmail: ResolverFn = async (

await checkNotificationUpdatePermissions(check, hasPermission)

await query(sqlClientPool, Sql.updateNotificationEmail(input));
try {
await query(sqlClientPool, Sql.updateNotificationMicrosoftTeams(input));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error renaming notification ${input.name}. Notification ${input.patch.name} already exists`
)
} else {
throw new Error(error.message);
}
};

const rows = await query(
sqlClientPool,
Sql.selectNotificationEmailByName(name)
Expand All @@ -592,7 +638,18 @@ export const updateNotificationRocketChat: ResolverFn = async (

await checkNotificationUpdatePermissions(check, hasPermission)

await query(sqlClientPool, Sql.updateNotificationRocketChat(input));
try {
await query(sqlClientPool, Sql.updateNotificationMicrosoftTeams(input));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error renaming notification ${input.name}. Notification ${input.patch.name} already exists`
)
} else {
throw new Error(error.message);
}
};

const rows = await query(
sqlClientPool,
Sql.selectNotificationRocketChatByName(name)
Expand All @@ -619,7 +676,18 @@ export const updateNotificationSlack: ResolverFn = async (

await checkNotificationUpdatePermissions(check, hasPermission)

await query(sqlClientPool, Sql.updateNotificationSlack(input));
try {
await query(sqlClientPool, Sql.updateNotificationMicrosoftTeams(input));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error renaming notification ${input.name}. Notification ${input.patch.name} already exists`
)
} else {
throw new Error(error.message);
}
};

const rows = await query(
sqlClientPool,
Sql.selectNotificationSlackByName(name)
Expand Down
47 changes: 29 additions & 18 deletions services/api/src/resources/problem/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,35 @@ export const addProblem: ResolverFn = async (
project: environment.project
});

const { insertId } = await query(
sqlClientPool,
Sql.insertProblem({
severity,
severity_score: severityScore,
lagoon_service: service || '',
identifier,
environment: environmentId,
source,
associated_package: associatedPackage,
description,
version: version || '',
fixed_version: fixedVersion,
links: links,
data,
created
})
);
let insertId: number;
try {
({insertId} = await query(
sqlClientPool,
Sql.insertProblem({
severity,
severity_score: severityScore,
lagoon_service: service || '',
identifier,
environment: environmentId,
source,
associated_package: associatedPackage,
description,
version: version || '',
fixed_version: fixedVersion,
links: links,
data,
created
})
));
} catch(error) {
if(error.text.includes("Duplicate entry")){
throw new Error(
`Error adding problem. Problem already exists.`
);
} else {
throw new Error(error.message);
}
};

const rows = await query(
sqlClientPool,
Expand Down
Loading

0 comments on commit 7716346

Please sign in to comment.