-
Notifications
You must be signed in to change notification settings - Fork 144
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
Replace dynamically generated errors with constants #711
Replace dynamically generated errors with constants #711
Conversation
Only errors that contain static text message were replaced.
Replace errors created with `errors.NewCallbackError()`.
gothemis/session/session.go
Outdated
ErrMissingClientID = errors.NewWithCode(errors.InvalidParameter, "empty client ID for Secure Session") | ||
ErrMissingPrivateKey = errors.NewWithCode(errors.InvalidParameter, "empty client private key for Secure Session") | ||
ErrMissingMessage = errors.NewWithCode(errors.InvalidParameter, "empty message for Secure Session") | ||
ErrOverflow = errors.NewWithCode(errors.NoMemory, "Secure Session cannot allocate enough memory") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear what exactly overflow. Memory, int capacity, buffer capacity, amount of requests.... Let's give the name which enough just to read and it will describe what exactly overflowed?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about ErrOverflow
-> ErrOutOfMemory
? Quite common name for such types of errors.
UPD: I guess it was called ErrOverflow
because in most all cases this error was returned when sizeOverflow()
returned false
(which means passed size couldn't fit in 32-bit signed int). BTW, I noticed this func implementation is duplicated few times:
cell/cell.go
235:func sizeOverflow(n C.size_t) bool {
message/message.go
108:func sizeOverflow(n C.size_t) bool {
compare/compare.go
104:func sizeOverflow(n C.size_t) bool {
session/session.go
80:func sizeOverflow(n C.size_t) bool {
keys/keypair.go
129:func sizeOverflow(n C.size_t) bool {
so maybe it's better to implement it once and use some imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it is much better. Advice for the future. It's good to search similar cases in the other code of project or in similar projects to not introduce something new and use the most common names/patterns/etc. Probably we used some errors like that in other parts of project)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, all these checks are for size overflow, but from Go viewpoint this can be treated as out-of-memory error. Themis Core asks Go to allocate a ginormous buffer with a length that can't even be represented with Go types, so Go cannot allocate that much memory.
Give errors more verbose names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these changes are introducing new public API or change existing API. I really want to get them straight as we'd have to live with the stupid constant names we choose now. The fewer we add or touch, the better. Here is some bikeshedding from my part. I normally do not use "request changes" unless the code is seriously broken, but in this case I'll keep it on until the naming is okay.
Also, as we do change the API, I guess this warrants a changelog entry.
gothemis/cell/cell.go
Outdated
ErrGetOutputSize = errors.New("Failed to get output size") | ||
ErrEncryptData = errors.New("Failed to protect data") | ||
ErrDecryptData = errors.New("Failed to unprotect data") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go's convention seems to start error messages with lowercase. There are some legacy error messages which do not follow this convention. I would ask you to fix the messages to use lowercase while extracting them into constants. (For avoidance of doubt, "Secure Cell" is a proper name so it should be capitalized.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still upper case error messages
gothemis/session/session.go
Outdated
ErrGetRequestSize = errors.New("Failed to get request size") | ||
ErrGenRequest = errors.New("Failed to generate request") | ||
ErrGetWrappedSize = errors.New("Failed to get wrapped size") | ||
ErrWrapData = errors.New("Failed to wrap data") | ||
ErrGetUnwrappedSize = errors.New("Failed to get unwrapped size") | ||
ErrUnwrapData = errors.New("Failed to unwrap data") | ||
ErrCallbackGetUnwrappedSize = errors.NewCallbackError("Failed to get unwraped size (get_public_key_by_id callback error)") | ||
ErrCallbackUnwrapData = errors.NewCallbackError("Failed to unwrap data (get_public_key_by_id callback error)") | ||
ErrCallbackGetRemoteIDLen = errors.NewCallbackError("Failed to get session remote id length") | ||
ErrCallbackBadRemoteIDLen = errors.NewCallbackError("Incorrect remote id length (0)") | ||
ErrCallbackGetRemoteID = errors.NewCallbackError("Failed to get session remote id") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I'd just squash all those variations into just two:
ErrMessageSize
— when we fail to get appropriate message sizeErrMessageData
— when processing something goes wrong
There may be some interest in whether the first or the second call to Themis Core failed, but I don't think that we need 10 new API entries to express basically the same thing: your data seems to be broken.
What do you think, everyone? (Naming is a discussion point too.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, we had some situations when we read logs and messages like that which include context where it occurs, could save our time) actually in Acra, where we can meet errors in keystore, which use secure cell, in transport package which uses secure session, in decryption module, which uses secure cell + secure message. And info about where it occurs, like could not process something in cell or message, or session, could help to understand part where was error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, there isn't really much use for having an error for each individual method.
Themis Core has a signle THEMIS_FAIL for all failures, not a bunch of
- THEMIS_SECURE_CELL_SEAL_ENCRYPT_FAIL
- THEMIS_SECURE_CELL_TOKEN_PROTECT_DECRYPT_FAIL
- THEMIS_SECURE_MESSAGE_VERIFY_FAIL
unique for each method. The context should be provided by a higher level means.
Discriminating between Secure Cell and Secure Message failures may be useful for a system where Secure Cell is used in only one subsystem and Secure Message is used in a different one. Then by observing the messages you can guess where the failure is. However, if multiple subsystems use Secure Cell then you still need some context for disambiguation – context added by the application, not Themis. And if you have this context in the first place, you don't really need to rely on Cell/Message distinction specifically to pinpoint the faulty subsystem.
* Update changelog * Deprecate ErrOverflow, use ErrOutOfMemory instead * Rename many errors, remove redundant ones
* Replace ErrGetRequestSize, ErrGetWrappedSize, ErrGetUnwrappedSize with ErrMessageSize * Replace ErrGenerateRequest, ErrWrapData, ErrUnwrapData with ErrMessageData
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I believe this is mergeable now. Thanks for tedious updates, @iamnotacake! 💪
@Lagovas could you please approve the PR or tell what exactly is still missing? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missed to change error messages to lowercase. after that lgtm
gothemis/cell/cell.go
Outdated
ErrGetOutputSize = errors.New("Failed to get output size") | ||
ErrEncryptData = errors.New("Failed to protect data") | ||
ErrDecryptData = errors.New("Failed to unprotect data") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still upper case error messages
gothemis/compare/compare.go
Outdated
ErrMissingSecret = errors.NewWithCode(errors.InvalidParameter, "empty secret for Secure Comparator") | ||
ErrMissingData = errors.NewWithCode(errors.InvalidParameter, "empty comparison message for Secure Comparator") | ||
ErrOverflow = errors.NewWithCode(errors.NoMemory, "Secure Comparator cannot allocate enough memory") | ||
ErrAppendSecret = errors.New("Failed to append secret") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
Only errors that contain static text message were replaced since they remain constant until the end of runtime.
Packages
cell
,compare
,keys
,message
,session
now contain more error constants, one may use them to compare with error returned by some function from the same package.ErrOverflow
will be deprecated starting from 0.14.0,ErrOutOfMemory
is recommended instead due to more understandable name.Checklist