Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there situations where we wouldn't want this behavior? I guess maybe people wouldn't appreciate their CI failing if the recording upload fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no idea why you wouldn't want this behaviour, but this is public/in the wild, and so making this anything other than opt-in could break users downstream, thus I'm adding the flag.

}

export interface SourcemapUploadOptions {
Expand Down
Loading