Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/lib/helpers/httpStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* determines the badge color based on HTTP status code
*
* @param statusCode
* @returns badge color
*/
export function getBadgeTypeFromStatusCode(
statusCode: number
): 'error' | 'warning' | 'success' | undefined {
if (statusCode >= 500) {
return 'error';
}

if (statusCode >= 400) {
return 'warning';
}

if (statusCode === 0) {
return undefined;
}

return 'success';
}
Comment on lines +7 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Harden mapping for invalid/edge values; simplify 0-check.

Guard against non-finite and negative status codes to avoid accidentally treating them as “success.” This also lets you drop the separate === 0 branch.

 export function getBadgeTypeFromStatusCode(
     statusCode: number
 ): 'error' | 'warning' | 'success' | undefined {
-    if (statusCode >= 500) {
+    // Treat non-finite or non-positive (0, negatives) as “no status”
+    if (!Number.isFinite(statusCode) || statusCode <= 0) {
+        return undefined;
+    }
+
+    if (statusCode >= 500) {
         return 'error';
     }
 
-    if (statusCode >= 400) {
+    if (statusCode >= 400) {
         return 'warning';
     }
 
-    if (statusCode === 0) {
-        return undefined;
-    }
-
     return 'success';
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function getBadgeTypeFromStatusCode(
statusCode: number
): 'error' | 'warning' | 'success' | undefined {
if (statusCode >= 500) {
return 'error';
}
if (statusCode >= 400) {
return 'warning';
}
if (statusCode === 0) {
return undefined;
}
return 'success';
}
export function getBadgeTypeFromStatusCode(
statusCode: number
): 'error' | 'warning' | 'success' | undefined {
// Treat non-finite or non-positive (0, negatives) as “no status”
if (!Number.isFinite(statusCode) || statusCode <= 0) {
return undefined;
}
if (statusCode >= 500) {
return 'error';
}
if (statusCode >= 400) {
return 'warning';
}
return 'success';
}
🤖 Prompt for AI Agents
In src/lib/helpers/httpStatus.ts around lines 7 to 23, the function currently
treats non-finite and negative status codes as "success" and keeps a separate
check for statusCode === 0; change the guard so any non-finite or non-positive
value returns undefined, then apply the usual >=500 => 'error', >=400 =>
'warning', else 'success'. Use Number.isFinite(statusCode) and check statusCode
> 0 to validate the input and remove the dedicated 0 branch.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
} from '@appwrite.io/pink-svelte';
import { timeFromNow, toLocaleDateTime } from '$lib/helpers/date';
import { capitalize } from '$lib/helpers/string';
import { getBadgeTypeFromStatusCode } from '$lib/helpers/httpStatus';
import { Copy } from '$lib/components';
import { logStatusConverter } from './store';
import { LogsRequest, LogsResponse } from '$lib/components/logs';
Expand Down Expand Up @@ -98,11 +99,9 @@
<Badge
content={selectedLog.responseStatusCode.toString()}
variant="secondary"
type={selectedLog?.responseStatusCode >= 400
? 'error'
: selectedLog.responseStatusCode === 0
? undefined
: 'success'} />
type={getBadgeTypeFromStatusCode(
selectedLog.responseStatusCode
)} />
</span>
</Layout.Stack>
<Layout.Stack gap="xs" inline>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import Sheet from './sheet.svelte';
import { capitalize } from '$lib/helpers/string';
import { calculateTime } from '$lib/helpers/timeConversion';
import { getBadgeTypeFromStatusCode } from '$lib/helpers/httpStatus';
import { logStatusConverter } from './store';
import DualTimeView from '$lib/components/dualTimeView.svelte';
import { func } from '../store';
Expand Down Expand Up @@ -96,11 +97,7 @@
{:else if column.id === 'responseStatusCode'}
<Badge
variant="secondary"
type={log.responseStatusCode >= 400
? 'error'
: log.responseStatusCode === 0
? undefined
: 'success'}
type={getBadgeTypeFromStatusCode(log.responseStatusCode)}
content={log.responseStatusCode.toString()} />
{:else if column.id === 'requestMethod'}
<Typography.Code size="m">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
} from '@appwrite.io/pink-svelte';
import { timeFromNow } from '$lib/helpers/date';
import { capitalize } from '$lib/helpers/string';
import { getBadgeTypeFromStatusCode } from '$lib/helpers/httpStatus';
import { Copy } from '$lib/components';
import { LogsRequest, LogsResponse } from '$lib/components/logs';
import { site } from '../store';
Expand Down Expand Up @@ -99,9 +100,9 @@
<Badge
content={selectedLog.responseStatusCode.toString()}
variant="secondary"
type={selectedLog?.responseStatusCode >= 400
? 'error'
: 'success'} />
type={getBadgeTypeFromStatusCode(
selectedLog.responseStatusCode
)} />
</span>
</Layout.Stack>
<Layout.Stack gap="xs" inline>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { calculateTime } from '$lib/helpers/timeConversion';
import { getBadgeTypeFromStatusCode } from '$lib/helpers/httpStatus';
import { Button } from '$lib/elements/forms';

export let columns: Column[];
Expand Down Expand Up @@ -87,7 +88,7 @@
<div>
<Badge
variant="secondary"
type={log.responseStatusCode >= 400 ? 'error' : 'success'}
type={getBadgeTypeFromStatusCode(log.responseStatusCode)}
content={log.responseStatusCode.toString()} />
</div>
{:else if column.id === 'requestPath'}
Expand Down