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

feat: adds try/catch block to increase resilience #666

Merged
merged 11 commits into from
Mar 18, 2024

Conversation

ankur0904
Copy link
Contributor

Description

  • Added try/catch block to the http adapter

Related issue(s)

Fixes #640

@ankur0904
Copy link
Contributor Author

@KhudaDad414
Please have a look :)

@ankur0904
Copy link
Contributor Author

Hi @KhudaDad414 I made the suggested changes. Please have a look and give your valuable suggestions.

@KhudaDad414
Copy link
Member

@ankur0904 can you reset the auto format changes? it's hard to know what changes you have made.

secondly the try/catch block should only be inconnect() method, and connect() should call _connect() inside try block. same with the send() method. send() method should have a try/catch block calling _send().

this is what I mean:

class Adapter {
    // This is your main public method for establishing a connection
    public async connect() {
        try {
            await this._connect();
        } catch (error) {
            // Handle or rethrow the error as needed
        }
    }

    // The actual connection logic is encapsulated here
    private async _connect() {
        // Your connection logic goes here
    }

    // The public method for sending data
    public async send() {
        try {
            await this._send(data);
        } catch (error) {
            // Handle error
        }
    }

    // The actual sending logic is handled privately here
    private async _send() {
        // Your sending logic goes here
    }
}

@ankur0904
Copy link
Contributor Author

@KhudaDad414 I tried to reset the auto format change. I have undo the last commit, removed the auto format manually, then recommitted the changes, got the merge conflict, and resolved the merge conflict.

I'll take your code as a reference and update the PR soon.

Thanks

@coveralls
Copy link

coveralls commented Jan 5, 2024

Pull Request Test Coverage Report for Build 7558836827

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 72.884%

Totals Coverage Status
Change from base Build 7540195680: 0.0%
Covered Lines: 388
Relevant Lines: 483

💛 - Coveralls

}

async _connect(): Promise<this> {
try {
Copy link
Member

Choose a reason for hiding this comment

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

we don't want to catch the error since we left the error handling the _connect method. errors thrown by this function will be handled by the connect method.

Suggested change
try {

Comment on lines 76 to 79
} catch (error) {
console.error('Error connecting to Redis:', error)
throw error
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} catch (error) {
console.error('Error connecting to Redis:', error)
throw error
}

@@ -164,6 +174,7 @@ class HttpAdapter extends Adapter {
}

async _connect(): Promise<this> {
try {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
try {

Comment on lines 187 to 190
} catch (error) {
console.error(error)
throw error
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} catch (error) {
console.error(error)
throw error
}

const scramSha512SecurityReq = securityRequirements.find(
(sec) => sec.type() === 'scramSha512'
)
async _connect() {
Copy link
Member

Choose a reason for hiding this comment

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

you have removed the public method. the functionality won't work anymore. connect method is used for error handling and _connect for the actual logic. we always call the _connect method from the connect method.

Comment on lines 252 to 255
} catch (error) {
console.error(error)
throw error
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} catch (error) {
console.error(error)
throw error
}

}

async _connect(): Promise<this> {
try {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
try {

Comment on lines 87 to 90
} catch (error) {
console.error('An error occurred while connecting:', error)
throw error
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} catch (error) {
console.error('An error occurred while connecting:', error)
throw error
}

}

private async _connect(): Promise<this> {
try{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
try{

Comment on lines 95 to 98
}catch (error) {
console.error('An error occurred while connecting:', error)
throw error
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}catch (error) {
console.error('An error occurred while connecting:', error)
throw error
}

@ankur0904
Copy link
Contributor Author

Sorry @KhudaDad414 for making the mistake. Now I fully understand that connect -> used for calling the _connect and handing the error & _connect for the business logic.

@ankur0904
Copy link
Contributor Author

I request you to please review the changes in src/adapters/kafka/index.ts this file.

@KhudaDad414
Copy link
Member

I request you to please review the changes in src/adapters/kafka/index.ts this file.

since this file doesn't have a _connect method, I suggest you move the business logic in _connect and them handle the error in the connect method. just like other adapters.

@ankur0904
Copy link
Contributor Author

@KhudaDad414 I added the connect function but I have some doubts about _connect function. Please have a look at the src/adapters/kafka/index.ts _connect function.

Copy link
Member

@KhudaDad414 KhudaDad414 left a comment

Choose a reason for hiding this comment

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

Left some comments.

return this._send(message)
} catch (error) {
console.error('Error sending message to Redis:', error)
throw error
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw error

return this._send(message)
} catch (error) {
console.error('Error sending message to Redis:', error)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
console.error('Error sending message to Redis:', error)
const errorMessage = `Failed to send message on channel '${message.channel}' to server '${message.serverName}'`;
this.emit('error', new Error(errorMessage));
this.emit('error', error)

})
return this
} catch (err) {
logWarningMessage(`Failed to Connect: An error occurred while connecting to '${this.name()}' on the '${this.channelNames}' channel. Please review the error details below for further information and corrective action.`)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
logWarningMessage(`Failed to Connect: An error occurred while connecting to '${this.name()}' on the '${this.channelNames}' channel. Please review the error details below for further information and corrective action.`)
logWarningMessage(`Failed to Connect: An error occurred while connecting to '${this.name()}'. Please review the error details below for further information and corrective action.`)

try{
return this._send(message)
} catch (error) {
console.error('Error sending message to HTTP:', error)
Copy link
Member

Choose a reason for hiding this comment

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

also add the channel and the server name to the error so it becomes easier to find the error.

try{
return this._send(message)
} catch(error){
console.error('Error sending message to MQTT:', error)
Copy link
Member

Choose a reason for hiding this comment

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

just like my previouse suggestion, please add the channel and the server where the error occured.

try{
return this._send(message)
} catch(error){
console.error('Error sending message to Socket.IO:', error)
Copy link
Member

Choose a reason for hiding this comment

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

same here.

try{
return this._send(message)
} catch(error){
console.error('Error sending message to WS:', error)
Copy link
Member

Choose a reason for hiding this comment

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

also here.

@KhudaDad414
Copy link
Member

@ankur0904 do you need some help here?

@ankur0904
Copy link
Contributor Author

I'm so sorry @KhudaDad414 for the late response. I have made the required changes in the latest commit.

Thanks

KhudaDad414
KhudaDad414 previously approved these changes Jan 15, 2024
Copy link
Member

@KhudaDad414 KhudaDad414 left a comment

Choose a reason for hiding this comment

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

LGTM!

@KhudaDad414
Copy link
Member

KhudaDad414 commented Jan 15, 2024

@ankur0904 the tests are failing.

@KhudaDad414 KhudaDad414 self-requested a review January 15, 2024 10:30
@ankur0904
Copy link
Contributor Author

@KhudaDad414 Please look at the updated changes.

@KhudaDad414
Copy link
Member

KhudaDad414 commented Jan 18, 2024

Hey @ankur0904 the SonarCloud is complaining about the code duplication. And it has a point. all of the code here is duplicated. 😆

let's make it simpler.
instead of handling the error in each adapter let's handle it where the connect and send functions are called.

for connect method:
https://github.com/KhudaDad414/glee/blob/bb511f62e332d6c0fa8510079ca2e10564c65104/src/lib/glee.ts#L154-L161

for send method:
https://github.com/KhudaDad414/glee/blob/bb511f62e332d6c0fa8510079ca2e10564c65104/src/lib/glee.ts#L280
https://github.com/KhudaDad414/glee/blob/bb511f62e332d6c0fa8510079ca2e10564c65104/src/lib/glee.ts#L217

@KhudaDad414
Copy link
Member

@ankur0904 just wrap the send inside a try/catch block and handle the error.

Copy link

sonarcloud bot commented Jan 24, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@ankur0904
Copy link
Contributor Author

ankur0904 commented Jan 24, 2024

Hey @ankur0904 the SonarCloud is complaining about the code duplication. And it has a point. all of the code here is duplicated. 😆

let's make it simpler. instead of handling the error in each adapter let's handle it where the connect and send functions are called.

for connect method: https://github.com/KhudaDad414/glee/blob/bb511f62e332d6c0fa8510079ca2e10564c65104/src/lib/glee.ts#L154-L161

for send method: https://github.com/KhudaDad414/glee/blob/bb511f62e332d6c0fa8510079ca2e10564c65104/src/lib/glee.ts#L280 https://github.com/KhudaDad414/glee/blob/bb511f62e332d6c0fa8510079ca2e10564c65104/src/lib/glee.ts#L217

@KhudaDad414
for send method:
error is already handled:
e.g.,

 a.instance.send(msg).catch((e: Error) => {
              this._processError(errorMiddlewares, e, msg)
})

&

this._clusterAdapter.instance.send(message).catch((e: Error) => {
        this._processError(this._router.getErrorMiddlewares(), e, message)
})

Copy link
Member

@KhudaDad414 KhudaDad414 left a comment

Choose a reason for hiding this comment

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

LGTM

@ankur0904
Copy link
Contributor Author

@KhudaDad414
Thanks for your valuable guidance throughout the process. Happy to work on another issue as well.

@KhudaDad414
Copy link
Member

/rtm

@asyncapi-bot asyncapi-bot merged commit 485068c into asyncapi:master Mar 18, 2024
11 checks passed
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.36.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Stability in Glee by Handling Message and Connection Failures
4 participants