Skip to content

Commit c686f53

Browse files
committed
Handle ProblemDetail errors in comment and reply forms
1 parent 44cfe88 commit c686f53

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

packages/comment-widget/src/comment-form.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
versionContext,
1919
} from './context';
2020
import type { ToastManager } from './lit-toast';
21+
import type { ProblemDetail } from './types';
2122
import {
2223
type CaptchaRequiredResponse,
2324
getCaptchaCodeHeader,
@@ -156,8 +157,14 @@ export class CommentForm extends LitElement {
156157
this.toastManager?.warn(detail);
157158
return;
158159
}
159-
}
160160

161+
const problemDetail = error.data as unknown as ProblemDetail;
162+
this.toastManager?.error(
163+
[problemDetail?.title, problemDetail?.detail].join(' - ') ||
164+
msg('Comment failed, please try again later')
165+
);
166+
return;
167+
}
161168
this.toastManager?.error(msg('Comment failed, please try again later'));
162169
} finally {
163170
this.submitting = false;

packages/comment-widget/src/reply-form.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
toastContext,
2121
} from './context';
2222
import type { ToastManager } from './lit-toast';
23+
import type { ProblemDetail } from './types';
2324
import {
2425
type CaptchaRequiredResponse,
2526
getCaptchaCodeHeader,
@@ -160,6 +161,13 @@ export class ReplyForm extends LitElement {
160161
this.toastManager?.warn(detail);
161162
return;
162163
}
164+
165+
const problemDetail = error.data as unknown as ProblemDetail;
166+
this.toastManager?.error(
167+
[problemDetail?.title, problemDetail?.detail].join(' - ') ||
168+
msg('Comment failed, please try again later')
169+
);
170+
return;
163171
}
164172

165173
this.toastManager?.error(msg('Comment failed, please try again later'));

packages/comment-widget/src/types/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ interface AvatarConfig {
3232
interface EditorConfig {
3333
placeholder?: string;
3434
}
35+
36+
export interface ProblemDetail {
37+
type: string;
38+
title: string;
39+
status: number;
40+
detail: string;
41+
instance: string;
42+
requestId: string;
43+
timestamp: string;
44+
}

0 commit comments

Comments
 (0)