Skip to content

Commit 3f85808

Browse files
committed
Fix: URL params & error msg
1 parent aacc7bc commit 3f85808

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Adds or updates a query parameter to the given URL.
3+
*
4+
* @param {string} url - The URL to which the query parameter will be added.
5+
* @param {string} key - The query parameter key to add or update.
6+
* @param {string} value - The value of the query parameter to add or update.
7+
* @return {string} - The updated URL with the new or updated query parameter, or an empty string if the URL is invalid.
8+
*/
9+
export default function addQueryArg( url: string, key: string, value: string ): string {
10+
try {
11+
const urlObject = new URL( url );
12+
urlObject.searchParams.set( key, value );
13+
14+
return urlObject.toString();
15+
} catch ( error ) {
16+
// eslint-disable-next-line no-console
17+
console.error( `Failed to add query param: "${ key }" to "${ url }".`, error );
18+
19+
return '';
20+
}
21+
}

web/packages/hovercards/src/core.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Placement } from './compute-position';
22
import computePosition from './compute-position';
33
import { escUrl, escHtml } from './sanitizer';
4+
import addQueryArg from './add-query-arg';
45
import __ from './i18n';
56

67
interface AccountData {
@@ -255,7 +256,7 @@ export default class Hovercards {
255256
const hovercard = dc.createElement( 'div' );
256257
hovercard.className = `gravatar-hovercard${ additionalClass ? ` ${ additionalClass }` : '' }`;
257258

258-
const trackedProfileUrl = escUrl( `${ profileUrl }?utm_source=hovercard` );
259+
const trackedProfileUrl = escUrl( addQueryArg( profileUrl, 'utm_source', 'hovercard' ) );
259260
const username = escHtml( displayName );
260261
const isEditProfile = ! description && myHash === hash;
261262
const renderSocialLinks = verifiedAccounts
@@ -411,7 +412,7 @@ export default class Hovercards {
411412

412413
this._onFetchProfileStart( hash );
413414

414-
fetch( `${ BASE_API_URL }/${ hash }?source=hovercard` )
415+
fetch( addQueryArg( `${ BASE_API_URL }/${ hash }`, 'source', 'hovercard' ) )
415416
.then( ( res ) => {
416417
// API error handling
417418
if ( res.status !== 200 ) {
@@ -457,12 +458,16 @@ export default class Hovercards {
457458
.catch( ( code ) => {
458459
let message = __( this._i18n, 'Sorry, we are unable to load this Gravatar profile.' );
459460

460-
if ( code === 429 ) {
461-
message = __( this._i18n, 'Too Many Requests.' );
462-
}
463-
464-
if ( code === 500 ) {
465-
message = __( this._i18n, 'Internal Server Error.' );
461+
switch ( code ) {
462+
case 404:
463+
message = __( this._i18n, 'Profile not found.' );
464+
break;
465+
case 429:
466+
message = __( this._i18n, 'Too Many Requests.' );
467+
break;
468+
case 500:
469+
message = __( this._i18n, 'Internal Server Error.' );
470+
break;
466471
}
467472

468473
const hovercardInner = Hovercards.createHovercardError(

0 commit comments

Comments
 (0)