|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | + |
| 23 | +package softassert |
| 24 | + |
| 25 | +import ( |
| 26 | + "go.temporal.io/server/common/log" |
| 27 | + "go.temporal.io/server/common/log/tag" |
| 28 | +) |
| 29 | + |
| 30 | +// That performs a soft assertion by logging an error if the given condition is false. |
| 31 | +// It is meant to indicate a condition that are always expected to be true. |
| 32 | +// Returns true if the condition is met, otherwise false. |
| 33 | +// |
| 34 | +// Example: |
| 35 | +// softassert.That(logger, object.state == "ready", "object is not ready") |
| 36 | +// |
| 37 | +// Best practices: |
| 38 | +// - Use it to check for programming errors and invariants. |
| 39 | +// - Use it to communicate assumptions about the code. |
| 40 | +// - Use it to abort or recover from an unexpected state. |
| 41 | +// - Never use it as a substitute for regular error handling, validation, or control flow. |
| 42 | +func That(logger log.Logger, condition bool, msg string) bool { |
| 43 | + if !condition { |
| 44 | + // By using the same prefix for all assertions, they can be reliably found in logs. |
| 45 | + logger.Error("failed assertion: "+msg, tag.FailedAssertion) |
| 46 | + } |
| 47 | + return condition |
| 48 | +} |
0 commit comments