Skip to content

Commit fd64504

Browse files
authored
fix(ui): split only on first colon in toast error messages (#15894)
## Summary Toast notifications truncate error messages that contain multiple colons. For example, `throw new APIError('With: multiple: colons', 400, undefined, true)` only displays "With: multiple" in the toast — everything after the second colon is dropped. ## Changes `createErrorsFromMessage` in `packages/ui/src/elements/Toasts/fieldErrors.tsx` used `message.split(':')` and destructured only the first two elements, discarding anything after the second colon. Replaced with `indexOf`/`slice` to split only on the first colon, so the full message is preserved. ## Testing ```bash pnpm vitest run packages/ui/src/elements/Toasts/fieldErrors.spec.ts ``` Without the fix, the test "should preserve the full message when it contains multiple colons" fails with: ``` - Expected: { errors: ['multiple: colons'], message: 'With: ' } + Received: { errors: ['multiple'], message: 'With: ' } ``` --------- Co-authored-by: German Jablonski <GermanJablo@users.noreply.github.com>
1 parent 36c051a commit fd64504

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { createErrorsFromMessage } from './fieldErrors.js'
4+
5+
describe('createErrorsFromMessage', () => {
6+
it('should return the full message when there is no colon', () => {
7+
const result = createErrorsFromMessage('Something went wrong')
8+
9+
expect(result).toEqual({ message: 'Something went wrong' })
10+
})
11+
12+
it('should split a single field error after the colon', () => {
13+
const result = createErrorsFromMessage('Validation failed: email')
14+
15+
expect(result).toEqual({ errors: ['email'], message: 'Validation failed: ' })
16+
})
17+
18+
it('should split multiple comma-separated field errors after the colon', () => {
19+
const result = createErrorsFromMessage('The following fields are invalid: email, name')
20+
21+
expect(result).toEqual({
22+
errors: ['email', 'name'],
23+
message: 'The following fields are invalid (2):',
24+
})
25+
})
26+
27+
it('should preserve the full message when it contains multiple colons', () => {
28+
const result = createErrorsFromMessage('With: multiple: colons')
29+
30+
expect(result).toEqual({ errors: ['multiple: colons'], message: 'With: ' })
31+
})
32+
33+
it('should replace " > " with " → " in error paths', () => {
34+
const result = createErrorsFromMessage('Invalid: parent > child')
35+
36+
expect(result).toEqual({ errors: ['parent → child'], message: 'Invalid: ' })
37+
})
38+
39+
it('should group similar errors and count them', () => {
40+
const result = createErrorsFromMessage('Invalid: blocks > 0, blocks > 1, other')
41+
42+
expect(result).toEqual({
43+
errors: ['blocks → 0', 'blocks → 1', 'other'],
44+
message: 'Invalid (3):',
45+
})
46+
})
47+
})

packages/ui/src/elements/Toasts/fieldErrors.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ function groupSimilarErrors(items: string[]): string[] {
2929
return result
3030
}
3131

32-
function createErrorsFromMessage(message: string): {
32+
export function createErrorsFromMessage(message: string): {
3333
errors?: string[]
3434
message: string
3535
} {
36-
const [intro, errorsString] = message.split(':')
36+
const colonIndex = message.indexOf(':')
37+
const intro = colonIndex >= 0 ? message.slice(0, colonIndex) : message
38+
const errorsString = colonIndex >= 0 ? message.slice(colonIndex + 1) : undefined
3739

3840
if (!errorsString) {
3941
return {

0 commit comments

Comments
 (0)