Skip to content

Commit 39da6c4

Browse files
committed
error handling
1 parent c74b4f3 commit 39da6c4

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

tooling/sparta/packages/discord/src/api/apiProvider.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { clientPromise } from "./axios";
22
import type { Client as ApiClient } from "@sparta/utils/openapi/types";
3+
import { logger } from "@sparta/utils";
34

45
/**
56
* Singleton class to provide access to the API client
@@ -30,11 +31,23 @@ export class ApiProvider {
3031
if (this.client) return; // Already initialized
3132

3233
try {
34+
logger.info("Initializing API client");
3335
this.client = await clientPromise;
36+
logger.info("API client initialized successfully");
3437
this.error = null;
3538
} catch (err) {
3639
this.error = err instanceof Error ? err : new Error(String(err));
37-
console.error("ApiProvider: Failed to initialize client:", err);
40+
logger.error(
41+
{
42+
error: this.error,
43+
message: this.error.message,
44+
stack: this.error.stack,
45+
apiUrl:
46+
process.env.VITE_APP_API_URL || "http://localhost:3000",
47+
apiKeyPresent: !!process.env.BACKEND_API_KEY,
48+
},
49+
"ApiProvider: Failed to initialize client"
50+
);
3851
}
3952
}
4053

@@ -44,7 +57,11 @@ export class ApiProvider {
4457
*/
4558
public getClient(): ApiClient {
4659
if (!this.client) {
47-
throw new Error("API client not initialized. Call init() first.");
60+
const error = new Error(
61+
"API client not initialized. Call init() first."
62+
);
63+
logger.error({ error }, "Failed to get API client");
64+
throw error;
4865
}
4966
return this.client;
5067
}

tooling/sparta/packages/discord/src/api/axios.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { OpenAPIClientAxios } from "openapi-client-axios";
22
import { apiDocs } from "@sparta/utils";
33
import { type Client } from "@sparta/utils/openapi/types";
4+
import { logger } from "@sparta/utils";
5+
6+
// Log environment variables related to API configuration
7+
logger.info(
8+
{
9+
apiUrl: process.env.VITE_APP_API_URL,
10+
nodeEnv: process.env.NODE_ENV,
11+
},
12+
"API client configuration"
13+
);
414

515
const apiInstance = new OpenAPIClientAxios({
616
// @ts-ignore
@@ -13,13 +23,20 @@ const apiInstance = new OpenAPIClientAxios({
1323
Accept: "application/json",
1424
"x-api-key": process.env.BACKEND_API_KEY,
1525
},
26+
// Adding option to allow absolute URLs
27+
allowAbsoluteUrls: true,
1628
},
1729
});
1830

1931
// Initialize and export the promise directly
2032
export const clientPromise = apiInstance
2133
.init<Client>()
2234
.then((client) => {
35+
// Log the configured base URL of the client
36+
logger.info(
37+
{ baseURL: apiInstance.getAxiosInstance().defaults.baseURL },
38+
"API client initialized"
39+
);
2340
return client;
2441
})
2542
.catch((err) => {

tooling/sparta/packages/discord/src/slashCommands/humans/verify.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ export async function handleVerifyCommand(
4545
const client = apiProvider.getClient();
4646

4747
try {
48+
// Log API configuration for debugging
49+
const apiConfig = {
50+
baseUrl:
51+
process.env.VITE_APP_API_URL || "http://localhost:3000",
52+
apiPath: `/api/users/discord/${userId}`,
53+
hasApiKey: !!process.env.BACKEND_API_KEY,
54+
apiKeyLength: process.env.BACKEND_API_KEY?.length,
55+
};
56+
logger.info({ apiConfig }, "API client configuration");
57+
4858
// Check if the user already exists
4959
const userResponse = await client.getUserByDiscordId({
5060
discordUserId: userId,
@@ -135,7 +145,19 @@ export async function handleVerifyCommand(
135145
return;
136146
}
137147

138-
const verificationUrl = `${publicFrontendUrl}/?verificationId=${verificationId}`;
148+
// Ensure the URL has the correct format
149+
let verificationUrl = `${publicFrontendUrl}`;
150+
if (verificationUrl.endsWith("/")) {
151+
verificationUrl = `${verificationUrl}?verificationId=${verificationId}`;
152+
} else {
153+
verificationUrl = `${verificationUrl}/?verificationId=${verificationId}`;
154+
}
155+
156+
// Log the verification URL (strip the ID for security)
157+
logger.info(
158+
{ verificationUrlBase: verificationUrl.split("?")[0] },
159+
"Created verification URL"
160+
);
139161

140162
// Create a button with the verification link
141163
const verifyButton = new ButtonBuilder()
@@ -159,7 +181,22 @@ export async function handleVerifyCommand(
159181
"Created verification session for user"
160182
);
161183
} catch (error: any) {
162-
logger.error(error, "Error handling passport verify command");
184+
logger.error(
185+
{
186+
error,
187+
errorMessage: error.message,
188+
stack: error.stack,
189+
code: error.code,
190+
status: error.status,
191+
config: error.config,
192+
baseUrl:
193+
process.env.VITE_APP_API_URL || "http://localhost:3000",
194+
backendApiKey: process.env.BACKEND_API_KEY
195+
? "present"
196+
: "missing",
197+
},
198+
"Error handling passport verify command"
199+
);
163200

164201
await interaction.reply({
165202
content:
@@ -168,7 +205,22 @@ export async function handleVerifyCommand(
168205
});
169206
}
170207
} catch (error: any) {
171-
logger.error(error, "Error handling passport verify command");
208+
logger.error(
209+
{
210+
error,
211+
errorMessage: error.message,
212+
stack: error.stack,
213+
code: error.code,
214+
status: error.status,
215+
config: error.config,
216+
baseUrl:
217+
process.env.VITE_APP_API_URL || "http://localhost:3000",
218+
backendApiKey: process.env.BACKEND_API_KEY
219+
? "present"
220+
: "missing",
221+
},
222+
"Error handling passport verify command"
223+
);
172224

173225
await interaction.reply({
174226
content:

tooling/sparta/terraform/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ resource "aws_ecs_task_definition" "sparta_api" {
492492
{ name = "L1_CHAIN_ID", value = var.l1_chain_id },
493493
{ name = "LOG_LEVEL", value = var.log_level },
494494
{ name = "LOG_PRETTY_PRINT", value = var.log_pretty_print ? "true" : "false" },
495-
{ name = "VITE_APP_API_URL", value = "http://${aws_lb.sparta_alb.dns_name}" },
495+
{ name = "VITE_APP_API_URL", value = "http://${aws_lb.sparta_alb.dns_name}/api" },
496496
{ name = "CORS_ALLOWED_ORIGINS", value = "http://${aws_lb.sparta_alb.dns_name}" },
497497
{ name = "USERS_TABLE_NAME", value = aws_dynamodb_table.sparta_users.name },
498498
{ name = "NODE_OPERATORS_TABLE_NAME", value = aws_dynamodb_table.sparta_node_operators.name }

0 commit comments

Comments
 (0)