Skip to content

Commit

Permalink
fix: add error object to formatErrorMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Nov 18, 2024
1 parent 7fcfa3e commit 3583f92
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 29 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
],
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
},
"parserOptions": {
"ecmaVersion": 2022,
Expand Down
1 change: 1 addition & 0 deletions src/js/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const MediaUIProps = {
MEDIA_CURRENT_TIME: 'mediaCurrentTime',
MEDIA_DURATION: 'mediaDuration',
MEDIA_ENDED: 'mediaEnded',
MEDIA_ERROR: 'mediaError',
MEDIA_ERROR_CODE: 'mediaErrorCode',
MEDIA_ERROR_MESSAGE: 'mediaErrorMessage',
MEDIA_FULLSCREEN_UNAVAILABLE: 'mediaFullscreenUnavailable',
Expand Down
2 changes: 0 additions & 2 deletions src/js/media-chrome-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from './utils/element-utils.js';
import { InvokeEvent } from './utils/events.js';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function getTemplateHTML(_attrs: Record<string, string>) {
return /*html*/ `
<style>
Expand Down Expand Up @@ -56,7 +55,6 @@ function getTemplateHTML(_attrs: Record<string, string>) {
`;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function getSlotTemplateHTML(_attrs: Record<string, string>) {
return /*html*/ `
<slot id="content"></slot>
Expand Down
14 changes: 12 additions & 2 deletions src/js/media-error-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function shouldOpenErrorDialog(errorCode?: number) {
return errorCode && errors[errorCode] !== null;
}

function formatErrorMessage(errorCode?: number, errorMessage?: string) {
function formatErrorMessage(errorCode?: number, errorMessage?: string, _error?: any) {
const message: string = errors[errorCode] ?? errorMessage ?? '';
const parts = message.split(':', 2);

Expand Down Expand Up @@ -70,6 +70,8 @@ class MediaErrorDialog extends MediaChromeDialog {
return [...super.observedAttributes, ...observedAttributes];
}

#mediaError = null;

attributeChangedCallback(attrName: string, oldValue: string | null, newValue: string | null) {
super.attributeChangedCallback(attrName, oldValue, newValue);

Expand All @@ -82,10 +84,18 @@ class MediaErrorDialog extends MediaChromeDialog {
this.shadowRoot.querySelector('slot').name = `error-${this.mediaErrorCode}`;
this.shadowRoot.querySelector('#content').innerHTML = (
this.constructor as typeof MediaErrorDialog
).formatErrorMessage(this.mediaErrorCode, this.mediaErrorMessage);
).formatErrorMessage(this.mediaErrorCode, this.mediaErrorMessage, this.mediaError);
}
}

get mediaError() {
return this.#mediaError;
}

set mediaError(value) {
this.#mediaError = value;
}

get mediaErrorCode() {
return getNumericAttr(this, 'mediaerrorcode');
}
Expand Down
10 changes: 10 additions & 0 deletions src/js/media-store/state-mediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export type FacadeProp<T, S = T, D = T> = ReadonlyFacadeProp<T, D> & {
export type StateMediator = {
mediaErrorCode: ReadonlyFacadeProp<MediaError['code']>;
mediaErrorMessage: ReadonlyFacadeProp<MediaError['message']>;
mediaError: ReadonlyFacadeProp<MediaError>;
mediaWidth: ReadonlyFacadeProp<number>;
mediaHeight: ReadonlyFacadeProp<number>;
mediaPaused: FacadeProp<HTMLMediaElement['paused']>;
Expand Down Expand Up @@ -286,6 +287,15 @@ export const prepareStateOwners = async (
};

export const stateMediator: StateMediator = {
mediaError: {
get(stateOwners) {
const { media } = stateOwners;
// Add additional error info via the `mediaError` element property only.
// This can be used in the MediaErrorDialog.formatErrorMessage() method.
return media?.error;
},
mediaEvents: ['emptied', 'error'],
},
mediaErrorCode: {
get(stateOwners) {
const { media } = stateOwners;
Expand Down
2 changes: 2 additions & 0 deletions src/js/media-store/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export const toggleSubtitleTracks = (stateOwners, force: boolean): void => {
export const areValuesEq = (x: any, y: any): boolean => {
// If both are strictly equal, they're equal
if (x === y) return true;
// If either is null, they're not equal
if (x == null || y == null) return false;
// If their types don't match, they're not equal
if (typeof x !== typeof y) return false;
// Treat NaNs as equal
Expand Down
29 changes: 5 additions & 24 deletions src/js/utils/server-safe-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ const documentShim = {
},
addEventListener() { },
removeEventListener() { },
/**
*
* @param {Event} event
* @returns {boolean}
*/
dispatchEvent(event /* eslint-disable-line @typescript-eslint/no-unused-vars */) {
dispatchEvent(_event: Event) {
return false;
},
} as unknown as typeof globalThis['document'];
Expand All @@ -57,22 +52,11 @@ const globalThisShim = {
whenDefined: function () { },
},
localStorage: {
/**
* @param {string} key
* @returns {string|null}
*/
getItem(key /* eslint-disable-line @typescript-eslint/no-unused-vars */) {
getItem(_key: string) {
return null;
},
/**
* @param {string} key
* @param {string} value
*/
setItem(key, value) { }, // eslint-disable-line @typescript-eslint/no-unused-vars
/**
* @param {string} key
*/
removeItem(key) { }, // eslint-disable-line @typescript-eslint/no-unused-vars
setItem(_key: string, _value: string) { },
removeItem(_key: string) { },
},
CustomEvent: function CustomEvent() { },
getComputedStyle: function () { },
Expand All @@ -82,10 +66,7 @@ const globalThisShim = {
return '';
},
},
/**
* @param {string} media
*/
matchMedia(media) {
matchMedia(media: string) {
return {
matches: false,
media,
Expand Down
67 changes: 67 additions & 0 deletions test/unit/media-store/util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { assert } from '@open-wc/testing';
import { areValuesEq } from '../../../src/js/media-store/util.js';

describe('areValuesEq', () => {
it('returns false on different nil values', () => {
assert(!areValuesEq(null, undefined));
});

it('returns true on equal nil values', () => {
assert(areValuesEq(null, null));
assert(areValuesEq(undefined, undefined));
});

it('returns false on different types', () => {
assert(!areValuesEq(1, '1'));
});

it('returns false on different values', () => {
assert(!areValuesEq(1, 2));
});

it('returns true on equal values', () => {
assert(areValuesEq(1, 1));
});

it('returns true on equal NaN values', () => {
assert(areValuesEq(NaN, NaN));
});

it('returns false on different number values', () => {
assert(!areValuesEq(NaN, 1));
});

it('returns false on different array lengths', () => {
assert(!areValuesEq([1], [1, 2]));
});

it('returns false on different array values', () => {
assert(!areValuesEq([1], [2]));
});

it('returns true on equal array values', () => {
assert(areValuesEq([1], [1]));
});

it('returns false on different object values', () => {
assert(!areValuesEq({ a: 1 }, { a: 2 }));
});

it('returns true on equal object values', () => {
assert(areValuesEq({ a: 1 }, { a: 1 }));
});

it('returns false on different object keys', () => {
assert(!areValuesEq({ a: 1 }, { b: 1 }));
});

it('returns false on different object key values', () => {
assert(!areValuesEq({ a: 1 }, { a: 2 }));
assert(!areValuesEq({ a: 1 }, null));
assert(!areValuesEq(null, { a: 2 }));
});

it('returns false on different object key types', () => {
assert(!areValuesEq({ a: 1 }, { a: '1' }));
});
});

0 comments on commit 3583f92

Please sign in to comment.