-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(headless SSR): improved error messaging for hook misuse (#4771)
Replace the previous cryptic error with a more meaningful and user-friendly message when a hook is used with an incorrect state provider. ### Before  ### Now  This change aims to improve developer experience by making debugging easier and faster. https://coveord.atlassian.net/browse/KIT-3783 --------- Co-authored-by: Alex Prudhomme <[email protected]>
- Loading branch information
Showing
6 changed files
with
88 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
import type {SolutionType} from '@coveo/headless/ssr-commerce'; | ||
import {capitalize} from './utils.js'; | ||
|
||
export class MissingEngineProviderError extends Error { | ||
static message = | ||
'Unable to find Context. Please make sure you are wrapping your component with either `StaticStateProvider` or `HydratedStateProvider` component that can provide the required context.'; | ||
'Unable to find Context. Make sure your component using a controller is wrapped in one of the following providers: `RecommendationProvider`, `ListingProvider`, `SearchProvider`, or `StandaloneProvider`'; | ||
constructor() { | ||
super(MissingEngineProviderError.message); | ||
} | ||
} | ||
|
||
export class UndefinedControllerError extends Error { | ||
static createEngineSupportMessage(solutionTypes: SolutionType[]) { | ||
const supportedEngineDefinitionList = solutionTypes.map( | ||
(solutionType) => `${solutionType}EngineDefinition` | ||
); | ||
return `This controller is only available in these engine definitions:\n${supportedEngineDefinitionList.map((def) => ` • ${def}`).join('\n')}`; | ||
} | ||
|
||
constructor(controllerName: string, solutionTypes: SolutionType[]) { | ||
super( | ||
[ | ||
`You're importing a controller (use${capitalize(controllerName)}) that is not defined in the current engine definition`, | ||
UndefinedControllerError.createEngineSupportMessage(solutionTypes), | ||
'', | ||
'Ensure that the component using the controller is wrapped in the appropriate State Provider.', | ||
// 'Learn more: TODO: Add link to documentation on how to use hooks with providers', | ||
].join('\n') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters