Is there a way to view which line is erroring in a dispatcher invoke code?

Hi there,

I am running a Dispatcher which handles JSON data. The build is connected to a dictionary of values the JSON looks up against, but on occasion, there will be values which are not present in the dictionary, which will cause the code to error.

The issue is that I do not know which line it is erroring on (and there are a lot of fields on the JSON, so manually checking is very long). Is there an easy way I can either tell which line is being processed when running or be told what the missing field is?

I found some code online, but it kept telling me that ‘line 0’ is where it errored, when not finding the key.

Any insight would be appreciated! Thank you.

@dr1992

You can use IsNothing(dict("Key")) to check if the key is present

also if its a dictionary you can use dict.Keys.Contains("Keyname") to check if the key is present

If this is not the query please elaborate so that we can give a better solution for your case

cheers

Thanks for your response, the issue is that we do not necessarily know which key is not present.

Also, excuse me being a noob, would I just add this code to the invoke block?

@dr1992

That is the reason we can check for key existence for all the keys that you want to look for and only when they exists you can use them…

These can be used in if conditions …these would return true or false depending on key is present or not in the given dictionary

cheers

scenario sounds like a list of Dictionary Keys are to validate dynamic and generic against a JSON holding Propeties with these key names.

If this understanding is correct, then we could some generic validation if all keys were present and/or which keys are missing.

Maybe you share some sample data, when more help is needed

Unfortunately I am unable to share this as it has sensitive data.

But let me give an eg:

  1. A customer enters their title/name into our website: Dr John Smith
  2. This then has the chance of going into several 3rd party portals, but they use different formatting, such as in drop downs, so Dr could be listed as Doctor on one or Doc on another
  3. The dictionary is used to establish which output is needed for each portal.

The issue: if they enter something which doesn’t match then it will give us a generic error for that block of code, but I currently cannot determine which field is being looked for.

maybe you can redact/anonymize it

From Dictionary part go though the different options like:

  • youDictVar.ContainsKey(YourKey)
  • yourDictVar.Keys - get all Keys
  • yourDictVar.Values- get all Values

And we can also work with Set Operations like Intersect, Except to find out what is common or not present, when checking different Sets (Set ~ Collection ~ Array ~ Lists…)

If you surround your code with Try Catch you can print the error message. For .NET Framework 5 or newer the missing key is also displayed in the error message:

image

Try
	'All your code here ...
	Dim dict As Dictionary(Of String, String) = new Dictionary(Of String, String)
	If (dict("banana") = "yellow") Then
		' Good banana
	End If
Catch e As Exception
	Console.WriteLine(e.Message)
	'Rethrow the exception
	Throw
End Try

I would like to try this but don’t really understand what the banana refers to aha. If it is specific fields in the dictionary, there are probably too many aha

1 Like

1/ Do not get confused by bananas. Just put all your invoke code in the “try” part.

2/ I was also strugning to find meaningfull explanation of error if it occured in invoke code. I found that such description can be found in InnerException attribute of the exception.

Cheers

1 Like

Just put your code in the Try-section. Ignore the banana code. It was just an example to trigger an exception. Replace it with your code instead.

Modified the code to print the inner exception message also as @J0ska recommended:

Try
	'All your code here ...
Catch e As Exception
	Console.WriteLine(e.Message)
	If e.InnerException IsNot Nothing Then
		Console.WriteLine("Inner exception: " + e.InnerException.Message)
	End If
	'Rethrow the exception
	Throw
End Try