forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error specific messages to product save functionality (woocommerc…
…e#38307) * Allow errors to be thrown when saving entity records * Show frontend error message when one exists * Allow errors to be thrown in preview save entity * Rename new util to getProductErrorMessage * Add tests around error message util * Add changelog entry
- Loading branch information
Showing
9 changed files
with
82 additions
and
16 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Add error specific messages to product save functionality |
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
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
20 changes: 20 additions & 0 deletions
20
packages/js/product-editor/src/utils/get-product-error-message.ts
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export type WPError = { | ||
code: string; | ||
message: string; | ||
data: { | ||
[ key: string ]: unknown; | ||
}; | ||
}; | ||
|
||
export function getProductErrorMessage( error: WPError ) { | ||
if ( error.code === 'product_invalid_sku' ) { | ||
return __( 'Invalid or duplicated SKU.', 'woocommerce' ); | ||
} | ||
|
||
return null; | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/js/product-editor/src/utils/test/get-product-error-message.ts
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getProductErrorMessage, WPError } from '../get-product-error-message'; | ||
|
||
describe( 'getProductErrorMessage', () => { | ||
it( 'should return the correct error message when one exists', () => { | ||
const error = { | ||
code: 'product_invalid_sku', | ||
} as WPError; | ||
const message = getProductErrorMessage( error ); | ||
expect( message ).toBe( 'Invalid or duplicated SKU.' ); | ||
} ); | ||
|
||
it( 'should return null when no error message exists', () => { | ||
const error = { | ||
code: 'unanticipated_error_code', | ||
} as WPError; | ||
const status = getProductErrorMessage( error ); | ||
expect( status ).toBeNull(); | ||
} ); | ||
} ); |