Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Decode HTML entities when formatting Store API error messages #5870

Merged
merged 6 commits into from
Apr 11, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Notice } from 'wordpress-components';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
Expand Down Expand Up @@ -50,7 +51,7 @@ const StoreNoticesContainer = ( { className, notices, removeNotice } ) => {
}
} }
>
{ props.content }
{ decodeEntities( props.content ) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @manospsyx - I discussed briefly with @xristos3490 and he suggested using decodeEntities in one place (here - the store notices container) rather than everywhere else we call createNotice

What do you think?

I tested it locally and it works as expected (with decodeEntities in assets/js/base/utils/errors.js removed). If you agree, we can revert errors.js and just keep the change in the container.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember there's at least one instance where decodeEntities is used outside -- I wasn't sure if that's intentional. I agree with Chris, but let's make sure that we're not double-decoding in other places. Can you check usage of decodeEntities in other locations to make sure?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed with @senadir over slack

My personal preference is to not call something unless needed seeing that you can't opt out of it in a clean way, I think decoding all server returned stings makes more sense than decoding at the createNotice level.

</Notice>
) ) }
</div>
Expand Down
14 changes: 7 additions & 7 deletions assets/js/base/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Given a JS error or a fetch response error, parse and format it so it can be displayed to the user.
Expand Down Expand Up @@ -48,11 +49,10 @@ export const formatStoreApiErrorMessage = ( response ) => {
}
}

return (
response?.message ||
__(
'Something went wrong. Please contact us to get assistance.',
'woo-gutenberg-products-block'
)
);
return response?.message
? decodeEntities( response.message )
: __(
'Something went wrong. Please contact us to get assistance.',
'woo-gutenberg-products-block'
);
};