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

Improve error handling in BytesQueue.Push function in queue.go #400

Open
akanchha25 opened this issue Jul 13, 2024 · 3 comments
Open

Improve error handling in BytesQueue.Push function in queue.go #400

akanchha25 opened this issue Jul 13, 2024 · 3 comments

Comments

@akanchha25
Copy link

The Push function in BytesQueue currently uses a generic error message when the queue is full. It is suggested to use errFullQueue for a more descriptive error message.

Current Code:

var (
	errEmptyQueue       = &queueError{"Empty queue"}
	errInvalidIndex     = &queueError{"Index must be greater than zero. Invalid index."}
	errIndexOutOfBounds = &queueError{"Index out of range"}
)

Push function

if !q.canInsertAfterTail(neededSize) {
		if q.canInsertBeforeHead(neededSize) {
			q.tail = leftMarginIndex
		} else if q.capacity+neededSize >= q.maxCapacity && q.maxCapacity > 0 {
			return -1, &queueError{"Full queue. Maximum size limit reached."}
		} else {
			q.allocateAdditionalMemory(neededSize)
		}
	}

Updated code

var (
	errEmptyQueue       = &queueError{"Queue is empty."}
	errInvalidIndex     = &queueError{"Index must be greater than zero. Invalid index."}
	errIndexOutOfBounds = &queueError{"Index out of range."}
	errFullQueue        = &queueError{"Queue is full. Maximum size limit reached."} 
)
if !q.canInsertAfterTail(neededSize) {
		if q.canInsertBeforeHead(neededSize)) {
			q.tail = leftMarginIndex
		} else if q.capacity+neededSize >= q.maxCapacity && q.maxCapacity > 0 {
			return -1, errFullQueue 
		} else {
			q.allocateAdditionalMemory(neededSize)
		}
	}

This will improve the code quality

@akanchha25 akanchha25 changed the title Improve error handling in BytesQueue.Push function by using descriptive error messages in queue.go Improve error handling in BytesQueue.Push function in queue.go Jul 13, 2024
@janisz
Copy link
Collaborator

janisz commented Jul 15, 2024

@akanchha25 Indeed it's a good improvement. Do you think errors should be unexpeorted? If we export them then they could be used to check for given error like with io.EOF

@akanchha25
Copy link
Author

@janisz That's a great point. Exporting the errors would indeed allow for more robust error handling, similar to checking for specific errors like io.EOF. This could be beneficial for users of the BytesQueue package to handle different error scenarios more explicitly.

I can update the code to export these error variables, making it easier for others to handle specific error conditions. I'll proceed with the changes if that sounds good to you.

@Vikram222726
Copy link

Vikram222726 commented Sep 15, 2024

Hi @akanchha25 @janisz . Guys i think bundling and seperating all error logs in a different go file like bytes_queue_error_logs.go would make it easy for us to keep all error logs in a single place and then export them directly in different functions or files for queue package. Would love to know your thoughts on this and if required, would like to work on adding this functionality if you could assign me this task.

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

No branches or pull requests

3 participants