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

crypto: use macro map for NodeCryptoError #37758

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,18 @@ void Decode(const v8::FunctionCallbackInfo<v8::Value>& args,
}
}

#define NODE_CRYPTO_ERROR_CODES_MAP(V) \
V(CIPHER_JOB_FAILED, "Cipher job failed") \
V(DERIVING_BITS_FAILED, "Deriving bits failed") \
V(ENGINE_NOT_FOUND, "Engine \"%s\" was not found") \
V(INVALID_KEY_TYPE, "Invalid key type") \
V(KEY_GENERATION_JOB_FAILED, "Key generation job failed") \
V(OK, "Ok") \

enum class NodeCryptoError {
CIPHER_JOB_FAILED,
DERIVING_BITS_FAILED,
ENGINE_NOT_FOUND,
INVALID_KEY_TYPE,
KEY_GENERATION_JOB_FAILED,
OK
#define V(CODE, DESCRIPTION) CODE,
NODE_CRYPTO_ERROR_CODES_MAP(V)
#undef V
};

// Utility struct used to harvest error information from openssl's error stack
Expand Down Expand Up @@ -194,28 +199,15 @@ template <typename... Args>
void CryptoErrorStore::Insert(const NodeCryptoError error, Args&&... args) {
const char* error_string = nullptr;
switch (error) {
case NodeCryptoError::CIPHER_JOB_FAILED:
error_string = "Cipher job failed";
break;
case NodeCryptoError::DERIVING_BITS_FAILED:
error_string = "Deriving bits failed";
break;
case NodeCryptoError::ENGINE_NOT_FOUND:
error_string = "Engine \"%s\" was not found";
break;
case NodeCryptoError::INVALID_KEY_TYPE:
error_string = "Invalid key type";
break;
case NodeCryptoError::KEY_GENERATION_JOB_FAILED:
error_string = "Key generation failed";
break;
case NodeCryptoError::OK:
error_string = "Ok";
break;
#define V(CODE, DESCRIPTION) \
case NodeCryptoError::CODE: error_string = DESCRIPTION; break;
NODE_CRYPTO_ERROR_CODES_MAP(V)
#undef V
}
errors_.emplace_back(SPrintF(error_string,
std::forward<Args>(args)...));
}
#undef NODE_CRYPTO_ERROR_CODES_MAP
Copy link
Member

Choose a reason for hiding this comment

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

Having this available outside of the header might be useful at some point 🤷‍♀️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@addaleax Agreed, exposed it in 6ce921a. Could you think of any place where it could be used currently? I could incorporate it in a follow-up PR.

Copy link
Member

Choose a reason for hiding this comment

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

No, not right now, although I guess if we add more error codes to CryptoErrorStore::Insert() then it might make sense to move the part that generates error_string/that doesn’t use args back into a .cc file (because right now every call to CryptoErrorStore::Insert() potentially creates a new copy of the function).


template <typename T>
T* MallocOpenSSL(size_t count) {
Expand Down