From 63bfad77ce4c36bb3fa7bc7d0ba75fe37c810119 Mon Sep 17 00:00:00 2001 From: Aarnav Tale Date: Thu, 3 Apr 2025 03:07:47 -0400 Subject: [PATCH] fix: add api-error file --- app/server/README.md | 1 + app/server/headscale/api-error.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 app/server/headscale/api-error.ts diff --git a/app/server/README.md b/app/server/README.md index 6cc9653..76d5b9b 100644 --- a/app/server/README.md +++ b/app/server/README.md @@ -19,6 +19,7 @@ server │ ├── schema.ts: Defines the schema for the Headplane configuration. ├── headscale/ │ ├── api-client.ts: Creates the HTTP client that talks to the Headscale API. +│ ├── api-error.ts: Contains the ResponseError definition. │ ├── config-loader.ts: Loads the Headscale configuration (if available). │ ├── config-schema.ts: Defines the schema for the Headscale configuration. ├── web/ diff --git a/app/server/headscale/api-error.ts b/app/server/headscale/api-error.ts new file mode 100644 index 0000000..bd87ee6 --- /dev/null +++ b/app/server/headscale/api-error.ts @@ -0,0 +1,19 @@ +// Represents an error that occurred during a response +// Thrown when status codes are >= 400 +export default class ResponseError extends Error { + status: number; + response: string; + responseObject?: Record; + + constructor(status: number, response: string) { + super(`Response Error (${status}): ${response}`); + this.name = 'ResponseError'; + this.status = status; + this.response = response; + + try { + // Try to parse the response as JSON to get a response object + this.responseObject = JSON.parse(response); + } catch {} + } +}