Skip to content

Commit 3d436a0

Browse files
committed
fix: some edge cases
1 parent 68c1d95 commit 3d436a0

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

backend/app/HTTP/Services/LogService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function save($status, $details, $message = null)
7979
}
8080

8181
$log->subject = Arr::get($details, 'subject', '');
82-
$log->to_addr = Arr::get($details, 'to', '');
82+
$log->to_addr = Arr::get($details, 'to', '[]');
8383

8484
unset($details['subject'], $details['to'], $details['from'], $details['phpmailer_exception_code']);
8585
$log->details = $details;

backend/db/Migrations/BitSmtpLogsTableMigration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function (Blueprint $table) {
2020
$table->id();
2121
$table->tinyint('status');
2222
$table->longtext('subject');
23-
$table->varchar('to_addr', 320);
23+
$table->longtext('to_addr');
2424
$table->longtext('details')->nullable();
2525
$table->text('debug_info')->nullable();
2626
$table->tinyint('retry_count')->defaultValue(0);

frontend/src/common/helpers/request.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import notify from '@components/Toaster/Toaster'
12
import config from '@config/config'
3+
import { __ } from './i18nwrap'
24

35
/* eslint-disable no-restricted-syntax */
46
/* eslint-disable no-undef */
@@ -73,8 +75,46 @@ export default async function request<T>({
7375
try {
7476
return JSON.parse(res)
7577
} catch (error) {
76-
const parsedRes = res.match(/{"code":(?:[^{}]*)*}/)
77-
return parsedRes ? JSON.parse(parsedRes[0]) : { code: 'ERROR', data: res }
78+
const parsedRes = extractJSON(res)
79+
const parsedData = (
80+
parsedRes === ''
81+
? { code: 'ERROR', message: __('Failed to parsed response'), data: res }
82+
: parsedRes
83+
) as Response<unknown>
84+
85+
if (parsedData.code === 'ERROR') {
86+
notify.error(parsedData.message as string)
87+
}
88+
89+
return parsedData
7890
}
7991
})) as Response<T>
8092
}
93+
94+
function extractJSON(str: string) {
95+
if (typeof str !== 'string') return null
96+
97+
let braceCount = 0
98+
let start = -1
99+
100+
for (let i = 0; i < str.length; i += 1) {
101+
if (str[i] === '{') {
102+
if (braceCount === 0) {
103+
start = i
104+
}
105+
braceCount += 1
106+
} else if (str[i] === '}') {
107+
braceCount -= 1
108+
if (braceCount === 0 && start !== -1) {
109+
const jsonStr = str.substring(start, i + 1)
110+
try {
111+
return JSON.parse(jsonStr)
112+
} catch (e) {
113+
return ''
114+
}
115+
}
116+
}
117+
}
118+
119+
return null
120+
}

frontend/src/pages/Layout/Header.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,7 @@ export default function Header() {
3030
{ label: __('Logs'), path: '/logs' },
3131
{ label: __('Others'), path: '/others' }
3232
]
33-
console.log(
34-
'Path---->Selected',
35-
navItems.find(item => {
36-
console.log(
37-
`item ${item.path}`,
38-
`path: ${location.pathname}`,
39-
`idx: ${location.pathname.includes(item.path)}`
40-
)
41-
return item.path === '/' ? item.path === location.pathname : location.pathname.includes(item.path)
42-
})
43-
)
33+
4434
const handleConfetti = () => {
4535
confetti({
4636
particleCount: 100,

0 commit comments

Comments
 (0)