Skip to content

Commit

Permalink
Add a 'strict' flag to have a recording upload fail if any part of th…
Browse files Browse the repository at this point in the history
…e upload fails. (#302)

* Add a 'strict' flag to have a recording upload fail if any part of the upload fails.

* lint
  • Loading branch information
klochek authored Jan 18, 2024
1 parent 9bdc808 commit aa45119
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
52 changes: 43 additions & 9 deletions packages/replay/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,38 @@ async function doUploadCrash(
client.closeConnection();
}

class RecordingUploadError extends Error {
interiorError?: any;

constructor(message?: string, interiorError?: any) {
super(message);
this.name = "RecordingUploadError";
this.interiorError = interiorError;
Object.setPrototypeOf(this, new.target.prototype); // Restore error prototype chain.
}
}

function handleUploadingError(err: string, strict: boolean, interiorError?: any) {
debug(err);

if (interiorError) {
debug(interiorError);
}

if (strict) {
throw new RecordingUploadError(err, interiorError);
}
}

async function doUploadRecording(
dir: string,
server: string,
recording: RecordingEntry,
verbose?: boolean,
apiKey?: string,
agent?: any,
removeAssets: boolean = false
removeAssets: boolean = false,
strict: boolean = false
) {
debug("Uploading %s from %s to %s", recording.id, dir, server);
maybeLog(verbose, `Starting upload for ${recording.id}...`);
Expand All @@ -183,8 +207,7 @@ async function doUploadRecording(

const reason = uploadSkipReason(recording);
if (reason) {
maybeLog(verbose, `Upload failed: ${reason}`);

handleUploadingError(reason, strict);
return null;
}

Expand All @@ -207,8 +230,7 @@ async function doUploadRecording(
debug("Uploading recording %o", recording);
const client = new ReplayClient();
if (!(await client.initConnection(server, apiKey, verbose, agent))) {
maybeLog(verbose, `Upload failed: can't connect to server ${server}`);

handleUploadingError(`Cannot connect to server ${server}`, strict);
return null;
}

Expand All @@ -226,8 +248,7 @@ async function doUploadRecording(
try {
await client.setRecordingMetadata(recordingId, metadata);
} catch (e) {
console.warn("Failed to set recording metadata");
console.warn(e);
handleUploadingError(`Failed to set recording metadata ${e}`, strict, e);
}
}

Expand Down Expand Up @@ -278,7 +299,11 @@ async function doUploadRecording(
{ concurrency: 5, stopOnError: false }
);
} catch (e) {
maybeLog(verbose, `can't upload sourcemap ${sourcemap.path} from disk: ${e}`);
handleUploadingError(
`Cannot upload sourcemap ${sourcemap.path} from disk: ${e}`,
strict,
e
);
}
},
{ concurrency: 10, stopOnError: false }
Expand Down Expand Up @@ -308,7 +333,16 @@ async function uploadRecording(id: string, opts: Options = {}) {
return null;
}

return doUploadRecording(dir, server, recording, opts.verbose, opts.apiKey, opts.agent, true);
return doUploadRecording(
dir,
server,
recording,
opts.verbose,
opts.apiKey,
opts.agent,
true,
opts.strict
);
}

async function processUploadedRecording(recordingId: string, opts: Options) {
Expand Down
5 changes: 5 additions & 0 deletions packages/replay/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface Options {
apiKey?: string;
verbose?: boolean;
agent?: any;

/**
* Fail the recording upload if any part of the upload fails.
*/
strict?: boolean;
}

export interface SourcemapUploadOptions {
Expand Down

0 comments on commit aa45119

Please sign in to comment.