Skip to content

Commit d0605bf

Browse files
fix: add error message for invalid query (#20090)
Co-authored-by: Gilberto Alvarado <[email protected]>
1 parent d7c19ee commit d0605bf

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Diff for: projects/assets/src/translations/en/common.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@
9090
"exactMatch": "{{ term }}",
9191
"empty": "Ask us anything"
9292
},
93-
"closeSearchPanel": "Close"
93+
"closeSearchPanel": "Close",
94+
"queryError": "Your search query is incorrectly formatted. Please remove special characters like \":\" and try again."
9495
},
9596
"sorting": {
9697
"date": "Date",

Diff for: projects/core/src/product/store/effects/product-search.effect.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SearchConfig } from '../../model/search-config';
1212
import { ProductActions } from '../actions/index';
1313
import * as fromEffects from './product-search.effect';
1414
import createSpy = jasmine.createSpy;
15+
import { GlobalMessageService } from '@spartacus/core';
1516

1617
const searchResult: ProductSearchPage = { products: [] };
1718
const suggestionList: Occ.SuggestionList = { suggestions: [] };
@@ -21,6 +22,9 @@ class MockProductSearchConnector {
2122
getSuggestions = createSpy().and.returnValue(of(suggestionList.suggestions));
2223
}
2324

25+
class MockGlobalMessageService implements Partial<GlobalMessageService> {
26+
add = createSpy();
27+
}
2428
describe('ProductSearch Effects', () => {
2529
let actions$: Observable<Action>;
2630
let effects: fromEffects.ProductsSearchEffects;
@@ -29,6 +33,7 @@ describe('ProductSearch Effects', () => {
2933
beforeEach(() => {
3034
TestBed.configureTestingModule({
3135
providers: [
36+
{ provide: GlobalMessageService, useClass: MockGlobalMessageService },
3237
{
3338
provide: ProductSearchConnector,
3439
useClass: MockProductSearchConnector,

Diff for: projects/core/src/product/store/effects/product-search.effect.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ import { LoggerService } from '../../../logger';
1212
import { tryNormalizeHttpError } from '../../../util/try-normalize-http-error';
1313
import { ProductSearchConnector } from '../../connectors/search/product-search.connector';
1414
import { ProductActions } from '../actions/index';
15+
import { HttpErrorModel } from '../../../model';
16+
import {
17+
GlobalMessageService,
18+
GlobalMessageType,
19+
} from '../../../global-message';
1520

1621
@Injectable()
1722
export class ProductsSearchEffects {
1823
protected logger = inject(LoggerService);
24+
protected globalMessageService = inject(GlobalMessageService);
1925

2026
searchProducts$: Observable<
2127
ProductActions.SearchProductsSuccess | ProductActions.SearchProductsFail
@@ -35,14 +41,27 @@ export class ProductsSearchEffects {
3541
action.auxiliary
3642
);
3743
}),
38-
catchError((error) =>
39-
of(
44+
catchError((error) => {
45+
const normalizedError: HttpErrorModel = tryNormalizeHttpError(
46+
error,
47+
this.logger
48+
);
49+
if (
50+
normalizedError?.details?.[0].type ===
51+
'ArrayIndexOutOfBoundsError'
52+
) {
53+
this.globalMessageService.add(
54+
{ key: 'searchBox.queryError' },
55+
GlobalMessageType.MSG_TYPE_ERROR
56+
);
57+
}
58+
return of(
4059
new ProductActions.SearchProductsFail(
41-
tryNormalizeHttpError(error, this.logger),
60+
normalizedError,
4261
action.auxiliary
4362
)
44-
)
45-
)
63+
);
64+
})
4665
);
4766
})
4867
)

0 commit comments

Comments
 (0)