Skip to content

Commit

Permalink
api: log error messages in application layer
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenzhonauer committed Oct 12, 2021
1 parent 9de7c81 commit a161d3e
Show file tree
Hide file tree
Showing 60 changed files with 343 additions and 148 deletions.
2 changes: 2 additions & 0 deletions api/src/global_permission_grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
new VError(bodyResult, "failed to grant global permission"),
);
reply.status(code).send(body);
request.log.error({ err: bodyResult }, "Invalid request body");
return;
}

Expand All @@ -126,6 +127,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
.catch((err) => {
const { code, body } = toHttpError(err);
reply.status(code).send(body);
request.log.error({ err }, "Error while granting global permission");
});
});
}
2 changes: 2 additions & 0 deletions api/src/global_permission_revoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
new VError(bodyResult, "failed to revoke global permission"),
);
reply.status(code).send(body);
request.log.error({ err: bodyResult }, "Invalid request body");
return;
}

Expand All @@ -125,6 +126,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while revoking global permission");
reply.status(code).send(body);
});
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/global_permissions_grant_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
const { code, body } = toHttpError(
new VError(bodyResult, "failed to grant all global permissions"),
);
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand Down Expand Up @@ -149,6 +150,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while granting all global permissions");
reply.status(code).send(body);
}
},
Expand Down
1 change: 1 addition & 0 deletions api/src/global_permissions_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while fetching global permisions");
reply.status(code).send(body);
}
},
Expand Down
1 change: 1 addition & 0 deletions api/src/group_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while fetching all groups");
reply.status(code).send(body);
});
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/group_member_add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi

if (Result.isErr(bodyResult)) {
const { code, body } = toHttpError(new VError(bodyResult, "failed to add user to group"));
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand Down Expand Up @@ -129,6 +130,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while adding user to group");
reply.status(code).send(body);
});
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/group_member_remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
const { code, body } = toHttpError(
new VError(bodyResult, "failed to remove user from group"),
);
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand Down Expand Up @@ -131,6 +132,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while removing user from group");
reply.status(code).send(body);
});
});
Expand Down
5 changes: 4 additions & 1 deletion api/src/group_permissions_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi

const groupId = request.query.groupId;
if (!isNonemptyString(groupId)) {
const message = "required query parameter `groupId` not present (must be non-empty string)";
reply.status(404).send({
apiVersion: "1.0",
error: {
code: 404,
message: "required query parameter `groupId` not present (must be non-empty string)",
message,
},
});
request.log.error({ err: message }, "Invalid request body");
return;
}

Expand All @@ -105,6 +107,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while fetching permissions of group");
reply.status(code).send(body);
}
},
Expand Down
1 change: 1 addition & 0 deletions api/src/notification_count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while counting notifications");
reply.status(code).send(body);
}
});
Expand Down
13 changes: 11 additions & 2 deletions api/src/notification_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,17 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
if (request.query.offset !== undefined) {
offset = parseInt(request.query.offset, 10);
if (isNaN(offset)) {
const message = "if present, the query parameter `offset` must be an integer";

reply.status(400).send({
apiVersion: "1.0",
error: {
code: 400,
message: "if present, the query parameter `offset` must be an integer",
message,
},
});
request.log.error({ err: message }, "Invalid request body");

return;
}
}
Expand All @@ -330,13 +334,16 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
if (request.query.limit !== undefined) {
limit = parseInt(request.query.limit, 10);
if (isNaN(limit) || limit <= 0) {
const message = "if present, the query parameter `limit` must be a positive integer";
reply.status(400).send({
apiVersion: "1.0",
error: {
code: 400,
message: "if present, the query parameter `limit` must be a positive integer",
message,
},
});
request.log.error({ err: message }, "Invalid request body");

return;
}
}
Expand All @@ -355,6 +362,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
limit === undefined ? undefined : offsetIndex + limit,
);

request.log.debug("Getting exposed Notifcations");
const exposedNotifications: ExposedNotification[] = [];
for (const notification of slice) {
const metadata = await getMetadata(ctx, user, notification, service);
Expand All @@ -381,6 +389,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while getting Notifications");
reply.status(code).send(body);
}
},
Expand Down
2 changes: 2 additions & 0 deletions api/src/notification_mark_read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi

if (Result.isErr(bodyResult)) {
const { code, body } = toHttpError(new VError(bodyResult, "failed to mark notification"));
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand All @@ -120,6 +121,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while marking notification as read");
reply.status(code).send(body);
}
},
Expand Down
2 changes: 2 additions & 0 deletions api/src/project_assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi

if (Result.isErr(bodyResult)) {
const { code, body } = toHttpError(new VError(bodyResult, "failed to assign project"));
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand All @@ -123,6 +124,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while assigning project");
reply.status(code).send(body);
});
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/project_budget_delete_projected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
const { code, body } = toHttpError(
new VError(bodyResult, "failed to delete projected budget"),
);
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand All @@ -147,6 +148,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while deleting projected budget");
reply.status(code).send(body);
});
},
Expand Down
2 changes: 2 additions & 0 deletions api/src/project_budget_update_projected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
const { code, body } = toHttpError(
new VError(bodyResult, "failed to update projected budget"),
);
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand All @@ -159,6 +160,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while updating projected budget");
reply.status(code).send(body);
});
},
Expand Down
2 changes: 2 additions & 0 deletions api/src/project_close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi

if (Result.isErr(bodyResult)) {
const { code, body } = toHttpError(new VError(bodyResult, "failed to close project"));
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand All @@ -116,6 +117,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while closing project");
reply.status(code).send(body);
});
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/project_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
throw new VError(result, "project.list failed");
}
const projects = result;
request.log.debug("Mapping intents and timestamp of projects");
return projects.map((project) => {
return {
log: project.log,
Expand Down Expand Up @@ -198,6 +199,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while listing projects");
reply.status(code).send(body);
});
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/project_permission_grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
new VError(bodyResult, "failed to grant project permission"),
);
reply.status(code).send(body);
request.log.error({ err: bodyResult }, "Invalid request body");
return;
}

Expand All @@ -139,6 +140,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while granting project permission");
reply.status(code).send(body);
});
},
Expand Down
2 changes: 2 additions & 0 deletions api/src/project_permission_revoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
const { code, body } = toHttpError(
new VError(bodyResult, "failed to revoke project permission"),
);
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand All @@ -139,6 +140,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while revoking project permission");
reply.status(code).send(body);
});
},
Expand Down
7 changes: 6 additions & 1 deletion api/src/project_permissions_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi

const projectId = request.query.projectId;
if (!isNonemptyString(projectId)) {
const message =
"required query parameter `projectId` not present (must be non-empty string)";
reply.status(404).send({
apiVersion: "1.0",
error: {
code: 404,
message: "required query parameter `projectId` not present (must be non-empty string)",
message,
},
});

request.log.error({ err: message }, "Invalid request body");
return;
}

Expand All @@ -107,6 +111,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while listing project permissios");
reply.status(code).send(body);
}
},
Expand Down
2 changes: 2 additions & 0 deletions api/src/project_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi

if (Result.isErr(bodyResult)) {
const { code, body } = toHttpError(new VError(bodyResult, "failed to update project"));
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
Expand Down Expand Up @@ -147,6 +148,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while updating Project");
reply.status(code).send(body);
});
});
Expand Down
7 changes: 6 additions & 1 deletion api/src/project_view_details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,17 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi

const projectId = request.query.projectId;
if (!isNonemptyString(projectId)) {
const message =
"required query parameter `projectId` not present (must be non-empty string)";
reply.status(404).send({
apiVersion: "1.0",
error: {
code: 404,
message: "required query parameter `projectId` not present (must be non-empty string)",
message,
},
});

request.log.error({ err: message }, "Invalid request body");
return;
}

Expand Down Expand Up @@ -246,6 +250,7 @@ export function addHttpHandler(server: FastifyInstance, urlPrefix: string, servi
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while getting project details");
reply.status(code).send(body);
}
},
Expand Down
Loading

0 comments on commit a161d3e

Please sign in to comment.