1 /******************************************************************************* 2 3 Utility intended as a replacement for `assert` to check for programming 4 errors and sanity violations in situations when neither removing the check 5 in -release mode nor bringing down the application by throwing an `Error` 6 is acceptable. 7 8 This module must have as few import dependencies as possible so that it can 9 be used in place of `assert` freely without introducing cyclic imports. 10 11 Copyright: Copyright (c) 2017 dunnhumby Germany GmbH. All rights reserved 12 13 License: 14 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 15 Alternatively, this file may be distributed under the terms of the Tango 16 3-Clause BSD License (see LICENSE_BSD.txt for details). 17 18 *******************************************************************************/ 19 20 module ocean.core.Verify; 21 22 /******************************************************************************* 23 24 Verifies that certain condition is met. 25 26 Params: 27 ok = boolean condition to check 28 msg = optional exception message 29 30 Throws: 31 SanityException if `ok` condition is `false`. 32 33 *******************************************************************************/ 34 35 public void verify ( bool ok, lazy string msg = "", 36 string file = __FILE__, int line = __LINE__ ) 37 { 38 static SanityException exc; 39 40 if (!ok) 41 { 42 if (exc is null) 43 exc = new SanityException(""); 44 45 exc.file = file; 46 exc.line = line; 47 exc.msg = msg; 48 49 throw exc; 50 } 51 } 52 53 unittest 54 { 55 try 56 { 57 verify(false); 58 } 59 catch (SanityException e) { } 60 61 verify(true); 62 } 63 64 /******************************************************************************* 65 66 Indicates some internal sanity violation in the app, essentially a less 67 fatal version of `AssertError`. 68 69 *******************************************************************************/ 70 71 public class SanityException : Exception 72 { 73 public this ( string msg, string file = __FILE__, int line = __LINE__ ) 74 { 75 super(msg, file, line); 76 } 77 }