Errors - Use common helper to generate log IDs #34209
Merged
+11
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Sometimes, when logging an error, you want to generate a unique ID for that error -- so that you can tell the user about it. The user will share this error-ID with a sysadmin.
This pattern is a workflow convenience -- the sysadmin can search the error-logs more easily if there's a distinctive ID.
Before
Two instances of the pattern. Both generate IDs with random alphanumerics, but they use different formulas.
After
Same formula. Helper function can be called in other places (where we might introduce similar pattern).
Comment
Between the two formulas...
random_bytes()is better thancreateRandom(). But 12 chars are better than 6 bytes. Allowing the full range of alphanumerics is nice (shorter IDs). Longer ID's make it harder for user+admin to communicate out-of-band (phone/SMS/chat/etc). Neither generator would be used for security-keys, but we're not doing that. We just need something that's unique enough to help the sysadmin filter through logs. Both are OK for the job.(I kept the one from APIv4.)