Skip to content

Commit

Permalink
more refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Sep 26, 2023
1 parent 7a22f3c commit f695c32
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions Lambdas/GHOAuth/OAuthLambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ struct GHOAuthHandler: LambdaHandler {
logger.debug("Received event: \(event)")

guard let code = event.queryStringParameters?["code"] else {
logger.error("Missing code query parameter")
await logErrorToDiscord("Missing code query parameter")
return .init(statusCode: .badRequest, body: "Missing code query parameter")
}

guard let state = event.queryStringParameters?["state"] else {
logger.error("No state found")
return .init(statusCode: .badRequest, body: "Error getting state query parameter")
logger.error("Missing state query parameter")
await logErrorToDiscord("Missing state query parameter")
return .init(statusCode: .badRequest, body: "Missing state query parameter")
}

logger.trace("Got code and state", metadata: [
Expand All @@ -91,6 +94,7 @@ struct GHOAuthHandler: LambdaHandler {
"error": "\(String(reflecting: error))",
"state": .string(event.queryStringParameters?["state"] ?? "")
])
await logErrorToDiscord("Error verifying state")
return .init(statusCode: .badRequest, body: "Error verifying state")
}

Expand All @@ -104,14 +108,18 @@ struct GHOAuthHandler: LambdaHandler {
)])
).guardSuccess()
} catch {
logger.warning("Received Discord error while updating interaction", metadata: [
await logErrorToDiscord(
"Received Discord error while updating interaction: \(String(reflecting: error))"
)
logger.error("Received Discord error while updating interaction", metadata: [
"error": "\(String(reflecting: error))"
])
}
}

func failure(_ error: String) async -> APIGatewayV2Response {
await updateInteraction(color: .red, description: error )
await logErrorToDiscord(error)
await updateInteraction(color: .red, description: error)
return .init(statusCode: .badRequest, body: error)
}

Expand Down Expand Up @@ -234,4 +242,24 @@ struct GHOAuthHandler: LambdaHandler {

return user
}

func logErrorToDiscord(_ error: String) async {
do {
try await discordClient.createMessage(
channelId: Constants.Channels.logs.id,
payload: .init(embeds: [.init(
description: """
Error in GHHooks Lambda:
>>> \(error)
""",
color: .red
)])
).guardSuccess()
} catch {
logger.warning("Received Discord error while logging", metadata: [
"error": "\(String(reflecting: error))"
])
}
}
}

0 comments on commit f695c32

Please sign in to comment.