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

Replace dynamically generated errors with constants #711

Merged

Conversation

iamnotacake
Copy link
Contributor

@iamnotacake iamnotacake commented Sep 1, 2020

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

Only errors that contain static text message were replaced.
Replace errors created with `errors.NewCallbackError()`.
@iamnotacake iamnotacake added the W-GoThemis 🐹 Wrapper: GoThemis, Go API label Sep 1, 2020
gothemis/cell/cell.go Outdated Show resolved Hide resolved
gothemis/compare/compare.go Outdated Show resolved Hide resolved
gothemis/compare/compare.go Outdated Show resolved Hide resolved
gothemis/keys/keypair.go Outdated Show resolved Hide resolved
gothemis/session/session.go Outdated Show resolved Hide resolved
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")
Copy link
Collaborator

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?)

Copy link
Contributor Author

@iamnotacake iamnotacake Sep 1, 2020

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

Copy link
Collaborator

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)

Copy link
Collaborator

@ilammy ilammy Sep 1, 2020

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.

@ilammy ilammy changed the title Replace dymanically generated errors with const ones Replace dynamically generated errors with constants Sep 1, 2020
Copy link
Collaborator

@ilammy ilammy left a 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/compare/compare.go Outdated Show resolved Hide resolved
Comment on lines 183 to 185
ErrGetOutputSize = errors.New("Failed to get output size")
ErrEncryptData = errors.New("Failed to protect data")
ErrDecryptData = errors.New("Failed to unprotect data")
Copy link
Collaborator

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.)

Copy link
Collaborator

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 Show resolved Hide resolved
gothemis/keys/symmetric.go Outdated Show resolved Hide resolved
gothemis/message/message.go Outdated Show resolved Hide resolved
gothemis/cell/cell.go Show resolved Hide resolved
Comment on lines 42 to 52
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")
Copy link
Collaborator

@ilammy ilammy Sep 1, 2020

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 size
  • ErrMessageData — 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.)

Copy link
Contributor

Choose a reason for hiding this comment

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

agree

Copy link
Collaborator

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.

Copy link
Collaborator

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.

gothemis/session/session.go Outdated Show resolved Hide resolved
gothemis/session/session.go Outdated Show resolved Hide resolved
* 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
CHANGELOG.md Outdated Show resolved Hide resolved
gothemis/cell/cell.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@ilammy ilammy left a 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! 💪

@iamnotacake
Copy link
Contributor Author

@Lagovas could you please approve the PR or tell what exactly is still missing?

Copy link
Collaborator

@Lagovas Lagovas left a 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

Comment on lines 183 to 185
ErrGetOutputSize = errors.New("Failed to get output size")
ErrEncryptData = errors.New("Failed to protect data")
ErrDecryptData = errors.New("Failed to unprotect data")
Copy link
Collaborator

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

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")
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

@iamnotacake iamnotacake merged commit 68a91e9 into cossacklabs:master Sep 5, 2020
@iamnotacake iamnotacake deleted the T977-use-statically-created-errors branch September 5, 2020 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
W-GoThemis 🐹 Wrapper: GoThemis, Go API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants