How to print a message when an assert fails, in Dart? - dart

We'd like to print a message when an assert() fails. Currently in Dart, an assert only takes a boolean. We'd like to give the developer explicit reasons and instructions for what to do when the assert fails.

As of Dart 1.22, assert() takes an optional message.
assert(configFile != null, "Tool config missing.");
If the assertion fails, it will produce something like the following:
Unhandled exception:
'file:///.../main.dart': Failed assertion: line 9 pos 10:
'configFile != null': Tool config missing.
#0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:33)
#1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:29)
#2 main (file:///.../main.dart:9:10)
Note that the error message includes the actual assertion (configFile != null).

Just to add, if you're executing a dart file via command line, you need to enable asserts as follows, see reference here:
dart --enable-asserts main.dart

There is an open issue with a workaround https://github.com/dart-lang/sdk/issues/6190#issuecomment-119103626
assert(() => test || throw "message");
I tried this but this way it doesn't work. A slightly modified working version
var test = false;
assert(test ? true : throw "message");
See also
https://groups.google.com/a/dartlang.org/forum/#!topic/core-dev/yNiTFYmtmwY
https://github.com/dart-lang/sdk/issues/24213
https://github.com/sethladd/dep_assert_with_optional_message/blob/master/proposal.md

Related

NeoVim - Check if a Vim function exists from Lua

I plan on using vim-plug with NeoVim. So, my init.lua file will have function calls such as
vim.fn['plug#begin'](vim.fn.stdpath('data') .. '/plugged')
vim.fn['plug#']('hoob3rt/lualine.nvim')
However, I don't want to assume vim-plug is definitely installed. I want my init.lua file to degrade gracefully if vim-plug is not installed, rather than throwing an error
E5113: Error while calling lua chunk: Vim:E117: Unknown function: plug#begin
stack traceback:
[C]: in function 'plug#begin'
/Users/andy/.config/nvim/init.lua:8: in main chunk
How can I check if the vim-plug functions exist before attempting to call them?
I tried print(vim.fn['plug#begin']) but that for some reason prints a non-null value: function: 0x0104ba36f0, even though the function doesn't exist.
I tried print(vim.fn['plug#begin']) but that for some reason prints a non-null value: function: 0x0104ba36f0, even though the function doesn't exist.
Presumably it's returning a function that throws the error you are getting. I would thus recommend using pcall:
local success, error = pcall(vim.fn['plug#begin'], vim.fn.stdpath('data') .. '/plugged')
if not success then --[[fail gracefully]] end
caveat: this will catch any error, so you'll probably want to perform some check like if error:find"Unknown function" then ... end to only catch this specific error.

How to get more information for an error generated in libcontainer/rootfs_linux.go?

Code used from github: https://github.com/opencontainers/runc/blob/master/libcontainer/rootfs_linux.go#L59
This is the if block to debug for our interest....
if err := mountToRootfs(m, config.Rootfs, config.MountLabel, hasCgroupns); err != nil {
return newSystemErrorWithCausef(err, "mounting %q to rootfs at %q", m.Source, m.Destination)
}
Here in the if condition, there is an error generated thru newSystemErrorWithCausef function and which internally uses generic_error.go, fmt.Sprintf to format the error string.
In our integration, only once it hit this if block. And in the process of getting more details, I wanted to troubleshoot and understand why this happened.
As of now tried using fmt.Printf(........) in line 55 and also just before if block of line 59.
And also tried putting above statement in very first line of prepareRootfs function. And then tried newSystemErrorWithCause(err, "some other message") in the first line of prepareRootfs function. Overall I dont see any information generated with my messages.
Please provide any information to understand more of this issue. Please note I am not looking for why this error might have been caused. I am looking for how to get more information from that function of prepareRootfs.

How to identify exactly where exception occurred in Jenkins pipeline?

In Jenkins pipeline build, sometimes I've seen null pointer or other exceptions like -
java.lang.NullPointerException: Cannot invoke method trim() on null object
Generally if we run Java program through IDE or command line, if an exception occurs we see at which line number the exception has occurred.
But with Jenkins build output console, it does not show the line number where the exception has occurred.
In this case, based on method name ie trim() from log, I check wherever trim() method is used. But as I've used it at multiple places in same method, it becomes difficult to identify exactly where error has occurred.
Another way is to add echo statements and re-run build and see where it gives this exception but this is time consuming.
Is there any better way/plugin using which I can identify at which line of pipeline code exception has occurred?
I don't really know if it's possible to show the exact line number, but you can wrap your code in try-catch statements and then show the exception info in the catch, like so:
try {
// line with trim()
catch (ex) {
println "Exception while trimming: $ex"
}

Why does F# interactive console not consider "assert (2=3)" as wrong?

I send this line to F# interactive console in Visual Studio
assert (2=3)
To my surprise, the console does not report errors, but
 
val it : unit = ()
Similarly, if I run
printf ("hello!")
assert (2=3)
printf ("hello2")
on an REPL, I got "hellohello2" without any error messages.
How could I make the F# interactive tell me 2=3 is wrong?
Under the cover, the assert keyword translates to a method call to the Debug.Assert method from the .NET library (see the method documentaiton). This has a conditional compilation attribute [Conditional("DEBUG")], meaning that the call is only included if the symbol DEBUG is defined.
By default, this is not the case in F# Interactive. You can do that by adding --define:DEBUG to the command line parameters for fsi.exe. This will be somewhere in the options of your editor, depending on what you are using. For example, in Visual Studio, you need something like this:
EDIT: How to do something like this if you do not want to modify the command line parameters? This really depends on what exactly behaviour you want. The default behaviour of assert is that it shows a message box where you can either terminate the program or ignore the error. You could do that using:
open System.Windows.Forms
let ensure msg b =
let res =
MessageBox.Show("Assertion failed: " + msg +
"\n\nDo you want to terminate the exection? Press 'Yes' " +
"to stop or 'No' to ignore the error.", "Assertion failed",
MessageBoxButtons.YesNo)
if res = DialogResult.Yes then exit 42
ensure "Mathematics is broken" (2=3)

Xcode 5 Documentation Error

I wanted to describe my code like this
/** Loads and parses the XML file*/
+(NSMutableArray *)loadXML:(NSString *)table;
But when I try to use the quick help for loadXML: Xcode crashes with this Error:
ASSERTION FAILURE in /SourceCache/IDEPlugins/IDEPlugins-5064/IDEQuickHelp/Models/IDEQuickHelpContentCreator.m:321
Details: Error creating XML document from clang-parsed comment block: Error Domain=NSXMLParserErrorDomain Code=73 "Line 1: invalid character in attribute value
Line 1: attributes construct error
Line 1: Couldn't find end of Start Tag Function
" UserInfo=0x7fc78fb98500 {NSLocalizedDescription=Line 1: invalid character in attribute value
Line 1: attributes construct error
Line 1: Couldn't find end of Start Tag Function
}
Could anyone help me please?
Sorry for my bad english
Delete the "LaunchScreen.xib" from your project and you are done.
Sounds like it's time to submit a bug report to Apple's bug reporter.
If you restart your computer and re-launch Xcode, does the crash occur again?

Resources