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

refactor: #197 Refactor to use Promises internally #200

Merged
merged 40 commits into from
May 9, 2024

Conversation

miquelbeltran
Copy link
Contributor

@miquelbeltran miquelbeltran commented May 7, 2024

refactor: #197 Refactor to use Promises internally

Description 📝

Type of change

  • Refactor, should not affect users but makes codebase nicer

Updates

  • Removed callback property from SendOptions internal type.
  • Refactor raygun.transport.ts to return a Promise instead of using a callback.
  • Adapt raygun.sync.worker.ts to the changes in raygun.transport.ts.
  • Adapt raygun.batch.ts to the changes in raygun.transport.ts.
  • Change public send() method to implement logic directly instead of depending on legacy sendWithCallback.
  • Change public sendWithCallback() to use new send() method internally.
  • Other smaller related cleanup

Test plan 🧪

  • Ensure tests pass.
  • Run on both examples

Author to check 👓

  • Project and all contained modules builds successfully
  • Self-/dev-tested
  • Unit/UI/Automation/Integration tests provided where applicable
  • Code is written to standards
  • Appropriate documentation written (code comments, internal docs)

Reviewer to check ✔️

  • Project and all contained modules builds successfully
  • Change has been dev-/reviewer-tested, where possible
  • Unit/UI/Automation/Integration tests provided where applicable
  • Code is written to standards
  • Appropriate documentation written (code comments, internal docs)

Base automatically changed from 146-async-await to develop May 7, 2024 21:17
@miquelbeltran miquelbeltran marked this pull request as ready for review May 8, 2024 09:37
Comment on lines +89 to +91
const errorMessage = `Error is too large to send to Raygun (${messageSize}kb)\nStart of error: ${startOfMessage}`;
console.error(`[Raygun4Node] ${errorMessage}`);
throw Error(errorMessage);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Throw error upwards if the reported error message is too big, otherwise users will not notice.
See issue #140

Comment on lines +244 to +257
.then((response) => {
const durationInMs = stopTimer();
debug(
`[raygun.ts] Successfully sent message (duration=${durationInMs}ms)`,
);
return response;
})
.catch((error) => {
const durationInMs = stopTimer();
debug(
`[raygun.ts] Error sending message (duration=${durationInMs}ms): ${error}`,
);
return error;
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrap the transport.send call with a then/catch so we can log our own error/success messages, but still send the results back to the end user.

This is what was done in wrappedCallback.

Comment on lines +264 to +282
sendWithCallback(
exception: Error | string,
customData?: CustomData,
callback?: Callback<IncomingMessage>,
request?: RequestParams,
tags?: Tag[],
) {
// call async send but redirect response to provided legacy callback
this.send(exception, customData, request, tags)
.then((response) => {
if (callback) {
callVariadicCallback(callback, null, response);
}
})
.catch((error) => {
if (callback) {
callVariadicCallback(callback, error, null);
}
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Legacy send with a callback, wraps the new async send call.

IMO should be removed in the future.

Comment on lines +162 to +179
test("send with expressHandler custom data", function (t) {
t.plan(1);
let client = new Raygun.Client().init({
apiKey: API_KEY,
});

client.expressCustomData = function () {
return { test: "data" };
};
client._send = client.send;
client.send = (err, data, params, tags) => {
client.send = client._send;
t.equal(data.test, "data");
t.end();
return Promise.resolve(null);
};
client.expressHandler(new Error(), {}, {}, function () {});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test is moved to raygun_async_send because express handler now uses async send internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test file should be removed when we remove sendWithCallback as well.

@miquelbeltran miquelbeltran changed the title refactor: #197 Refactor to use Promises internally (WIP) refactor: #197 Refactor to use Promises internally May 8, 2024
@miquelbeltran miquelbeltran requested review from a team, TheRealAgentK, PanosNB and sumitramanga and removed request for a team May 8, 2024 10:23
Copy link
Collaborator

@sumitramanga sumitramanga left a comment

Choose a reason for hiding this comment

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

From a quick glance, this LGTM!

@miquelbeltran
Copy link
Contributor Author

thanks @sumitramanga ! I will wait to merge until 0.15.0-0 is released

@miquelbeltran miquelbeltran merged commit d8ec309 into develop May 9, 2024
10 checks passed
@miquelbeltran miquelbeltran deleted the 197-transport-promises branch May 9, 2024 06:26
@miquelbeltran miquelbeltran mentioned this pull request May 23, 2024
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants