Commit 6dea7a86 authored by Hendrik Garske's avatar Hendrik Garske

Fix: Verbesserte Fehlerbehandlung fuer WG API Endpunkte

parent 0c63f7a4
...@@ -27,12 +27,33 @@ export async function POST(request: NextRequest) { ...@@ -27,12 +27,33 @@ export async function POST(request: NextRequest) {
) )
} }
try {
const client = await wgApi.createClient({ const client = await wgApi.createClient({
name: name.trim(), name: name.trim(),
allowedIPs: allowedIPs || undefined, allowedIPs: allowedIPs || undefined,
}) })
return NextResponse.json(client, { status: 201 }) return NextResponse.json(client, { status: 201 })
} catch (apiError) {
console.error("WG API Error:", apiError)
const errorMessage = apiError instanceof Error ? apiError.message : "Failed to create client"
// Check if it's a 404 - API endpoint might be wrong
if (errorMessage.includes('404') || errorMessage.includes('not found')) {
return NextResponse.json(
{
error: "WireGuard API endpoint not found. Please check WG_API_URL configuration.",
details: errorMessage
},
{ status: 503 }
)
}
return NextResponse.json(
{ error: errorMessage },
{ status: 500 }
)
}
} catch (error) { } catch (error) {
console.error("Error creating client:", error) console.error("Error creating client:", error)
const errorMessage = error instanceof Error ? error.message : "Failed to create client" const errorMessage = error instanceof Error ? error.message : "Failed to create client"
......
...@@ -66,6 +66,8 @@ export class WgApiClient { ...@@ -66,6 +66,8 @@ export class WgApiClient {
headers['Authorization'] = `Basic ${Buffer.from(`:${this.password}`).toString('base64')}` headers['Authorization'] = `Basic ${Buffer.from(`:${this.password}`).toString('base64')}`
} }
console.log(`[WG API] Requesting: ${url}`)
const response = await fetch(url, { const response = await fetch(url, {
...options, ...options,
headers, headers,
...@@ -73,12 +75,22 @@ export class WgApiClient { ...@@ -73,12 +75,22 @@ export class WgApiClient {
if (!response.ok) { if (!response.ok) {
const errorText = await response.text() const errorText = await response.text()
throw new Error(`WG API Error: ${response.status} ${errorText}`) // Check if it's HTML (404 page) - means endpoint doesn't exist
if (errorText.includes('<!DOCTYPE html>') || errorText.includes('<html')) {
throw new Error(`WG API Endpoint not found: ${endpoint} (404). Check if WG_API_URL is correct: ${this.baseUrl}`)
}
throw new Error(`WG API Error: ${response.status} ${errorText.substring(0, 200)}`)
} }
const contentType = response.headers.get('content-type')
if (contentType && contentType.includes('application/json')) {
return response.json() return response.json()
} }
// If not JSON, return as text
return response.text() as unknown as T
}
async getStats(): Promise<WgStats> { async getStats(): Promise<WgStats> {
try { try {
return await this.request<WgStats>('/api/sessions') return await this.request<WgStats>('/api/sessions')
...@@ -111,7 +123,8 @@ export class WgApiClient { ...@@ -111,7 +123,8 @@ export class WgApiClient {
} }
async createClient(data: CreateClientRequest): Promise<CreateClientResponse> { async createClient(data: CreateClientRequest): Promise<CreateClientResponse> {
return this.request<CreateClientResponse>('/api/users', { // wg-easy uses /api/users endpoint
return await this.request<CreateClientResponse>('/api/users', {
method: 'POST', method: 'POST',
body: JSON.stringify(data), body: JSON.stringify(data),
}) })
...@@ -141,12 +154,19 @@ export class WgApiClient { ...@@ -141,12 +154,19 @@ export class WgApiClient {
headers['Authorization'] = `Basic ${Buffer.from(`:${this.password}`).toString('base64')}` headers['Authorization'] = `Basic ${Buffer.from(`:${this.password}`).toString('base64')}`
} }
const response = await fetch(`${this.baseUrl}/api/users/${id}/configuration`, { const url = `${this.baseUrl}/api/users/${id}/configuration`
console.log(`[WG API] Fetching config from: ${url}`)
const response = await fetch(url, {
headers, headers,
}) })
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to get config: ${response.status}`) const errorText = await response.text()
if (errorText.includes('<!DOCTYPE html>') || errorText.includes('<html')) {
throw new Error(`Config endpoint not found (404). Check if WG_API_URL is correct: ${this.baseUrl}`)
}
throw new Error(`Failed to get config: ${response.status} ${errorText.substring(0, 200)}`)
} }
return response.text() return response.text()
...@@ -158,12 +178,19 @@ export class WgApiClient { ...@@ -158,12 +178,19 @@ export class WgApiClient {
headers['Authorization'] = `Basic ${Buffer.from(`:${this.password}`).toString('base64')}` headers['Authorization'] = `Basic ${Buffer.from(`:${this.password}`).toString('base64')}`
} }
const response = await fetch(`${this.baseUrl}/api/users/${id}/qr-code`, { const url = `${this.baseUrl}/api/users/${id}/qr-code`
console.log(`[WG API] Fetching QR code from: ${url}`)
const response = await fetch(url, {
headers, headers,
}) })
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to get QR code: ${response.status}`) const errorText = await response.text()
if (errorText.includes('<!DOCTYPE html>') || errorText.includes('<html')) {
throw new Error(`QR code endpoint not found (404). Check if WG_API_URL is correct: ${this.baseUrl}`)
}
throw new Error(`Failed to get QR code: ${response.status} ${errorText.substring(0, 200)}`)
} }
return response.text() return response.text()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment