Skip to content

Commit

Permalink
Merge pull request #53 from THEOplayer/feature/conviva/additional_err…
Browse files Browse the repository at this point in the history
…or_logging

Feature/conviva/additional error logging
  • Loading branch information
tvanlaerhoven authored Oct 15, 2024
2 parents bfb8b30 + 69055eb commit e8480a1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-mugs-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theoplayer/conviva-connector-web": minor
---

Added additional error details on playback failure.
20 changes: 7 additions & 13 deletions conviva/src/integration/ConvivaHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import {
collectContentMetadata,
collectDeviceMetadata,
collectPlayerInfo,
flattenAndStringifyObject
flattenErrorObject
} from '../utils/Utils';
import { AdReporter } from './ads/AdReporter';
import { YospaceAdReporter } from './ads/YospaceAdReporter';
import { VerizonAdReporter } from './ads/VerizonAdReporter';
import { ErrorEvent } from 'theoplayer';

export interface ConvivaConfiguration {
customerKey: string;
Expand Down Expand Up @@ -279,25 +280,18 @@ export class ConvivaHandler {
this.convivaVideoAnalytics?.reportPlaybackMetric(Constants.Playback.SEEK_ENDED);
};

private readonly onError = () => {
private readonly onError = (errorEvent: ErrorEvent) => {
const metadata: ConvivaMetadata = {};
if (Number.isNaN(this.player.duration)) {
metadata[Constants.DURATION] = -1;
}
const error = this.player.errorObject;
const error = errorEvent.errorObject;

// Optionally report error details, which should be a flat {[key: string]: string} object.
if (error?.cause) {
try {
const errorDetails = flattenAndStringifyObject(error?.cause);
if (Object.keys(errorDetails).length > 0) {
this.convivaVideoAnalytics?.reportPlaybackEvent('ErrorDetailsEvent', errorDetails);
}
} catch (ignore) {
// Failed to stringify body
}
const errorDetails: Record<string, any> = flattenErrorObject(error);
if (Object.keys(errorDetails).length > 0) {
this.convivaVideoAnalytics?.reportPlaybackEvent('ErrorDetailsEvent', errorDetails);
}

this.convivaVideoAnalytics?.reportPlaybackFailed(error?.message ?? 'Fatal error occurred', metadata);

this.releaseSession();
Expand Down
30 changes: 17 additions & 13 deletions conviva/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
type GoogleImaAd,
type VerizonMediaAd,
type VerizonMediaAdBreak,
type THEOplayerError,
ErrorCategory,
ErrorCode,
version
} from 'theoplayer';
import { ConvivaConfiguration } from '../integration/ConvivaHandler';
Expand Down Expand Up @@ -220,18 +223,19 @@ export function calculateBufferLength(player: ChromelessPlayer): number {
return bufferLength * 1000;
}

export function flattenAndStringifyObject(obj: any): { [key: string]: string } {
const result: Record<string, string> = {};
Object.keys(obj).forEach((key) => {
try {
if (typeof obj[key] === 'object' && obj[key] !== null) {
result[key] = JSON.stringify(obj[key]);
} else {
result[key] = obj[key].toString();
}
} catch (ignore) {
// Failed to stringify value.
export function flattenErrorObject(error?: THEOplayerError): { [key: string]: string } {
const errorDetails: { [key: string]: string | undefined } = {
code: ErrorCode[error?.code ?? -1],
category: ErrorCategory[error?.category ?? -1],
name: error?.cause?.name,
message: error?.cause?.message,
stack: error?.stack
};
// Remove undefined values
for (const key in errorDetails) {
if (errorDetails[key] === undefined) {
delete errorDetails[key];
}
});
return result;
}
return errorDetails as { [key: string]: string };
}

0 comments on commit e8480a1

Please sign in to comment.