String.Contains doesn't work on web exception because it has a small dots between the words - string

I am checking if a web exception message contains a string I have.
My string is: "The remote name could not be resolved"
The web exception message is:"The remote name could not be resolved:"
You would expect it to work, but if you copy-paste and enlarge the web exception here you will notice tiny dots between every word.
Those dots apparently ruins the contains function I use.
If exWeb.Message.Contains("The remote name could not be resolved") Then
'Do something...
End If
How can I compare those strings without the dots or ignore the dots or any other solution?

Relying on the message string isn't very future proof, as the framework developers may decide to change the message in the future for various reasons (to make it more explanatory, change grammar .etc.) or your code could be running in a different locale with a different language causing the error message to be different.
I suggest you check the Status property instead.
See the documentation of the Enum. I think you're looking for a WebException with a Status of NameResolutionFailure
https://msdn.microsoft.com/en-us/library/system.net.webexceptionstatus(v=vs.110).aspx

Related

Can ArchUnit check for certain string patterns in method calls?

In our code we again and again have the issue that somebody forgot to adapt the usage of placeholders when switching between the use of the logger and String.format(...) methods.
For log statements one has to use '{}' as placeholders, like so:
logger.info("File {} successfully opened: {} bytes read, {} objects created", file, nrBytes, nrObjects);
But when using String.format(...) to compose a message one has to use '%s' as placeholders for strings and the statement has to read:
logger.info(String.format("File %s successfully opened: %s bytes read, %s objects created", file, nrBytes, nrObjects));
The second form is often used, when logging an error where the second argument is the Throwable that one wants to log.
Too often people forget about this details and then we end up with wrong log statements that output nothing reasonable.
I know and agree that this is absolutely not an architecture issue but rather a simple programming error, but it would be great if one could (ab-)use ArchUnit to check for the use of '%s' (or the absence of '{}') in the first String argument of the String.format()-method. Is something like that possible?
The ArchUnit, currently in version 0.16.0, does not analyze parameter values for method calls.
The sonar rule "Printf-style format strings should be used correctly" might however catch these bugs.
As already noted ArchUnit can't do this - PMD's [invalidlogmessageformat][1] rule is useful though (and I find PMD easier to deal with than sonar).

String to XNamespace Implicit Conversion inside a BizTalk Orchestration

I am a bit confused by what I see and hence headed over to SO.
I am developing a BizTalk (2010) Orchestration and I am wanting to parse an incoming XML message. I just need to retrieve then number of times a particular node is repeating. I could have used XPath. But, I chose to use LinqToXml.
I have created a variable of type System.Xml.Linq.XNamespace and inside an expression shape, I am assignning it a string value.. say http://mycompany/v1.0. This is a perfectly valid C# statment, as there is an implicit conversion from String to XNamespace (MSDN link).
But the Orchestration will not compile at all. I get this error cannot implicitly convert type System.String to System.Xml.Linq.XNamespace.
And if I dont use the XNamespace variable and directly run LinqToXml on the incoming message like this
MessageCount = MyXElement.Elements("{http://mycompany/v1.0}ListOfNotifications").Elements("{http://mycompany/v1.0}Notification").Count();
I get a cannot convert from String to XName error. Even this is confusing.
I am using BizTalk 2010 and C# 4.0. Can someone explain if I am missing something? I have tried all these code snippets using LinqPad and I get the expected response. So, there are no typos or missing references.
I opted to use the XPath option to retrieve the values that I needed, instead of using LinqToXml. The code that I ended up writing looks like below:
xpath(myOrchVariable, "string(/*[local-name()='InputRootNode' and namespace-uri()='http://my/name/space'])")

Groovy how to multi line GStrings for exception messages

What is the standard (or best practice) for Groovy error messages that that shouldn't span over a certain number of characters/line, e.g., 80 characters?
Consider the following (which is working fine)
throw new IOException("""\
A Jenkins configuration for the given version control
system (${vcs.name}) does not exist."""
.stripIndent()
.replaceAll('\n', ' '))
This will result in a one-line error message with no indention characters (what I want). But is there some other way ("the Groovy way of doing it") how to achieve this? If not, how could you add such a method to the GString class in a standalone Groovy application (if found hints regarding a Bootstrap.groovy file but it seems to be related to Grails)?
Example: """Consider a multi line string as shown above""".toSingleLine()
You could use the String continuation character then strip multiple spaces:
throw new IOException( "A Jenkins configuration for the given version control \
system (${vcs.name}) does not exist.".replaceAll( /( )\1+/, '$1' ) )
Or you could wrap this in a function and add it to the String.metaClass as I believe the answers you've seen point to.
You're right in thinking that Bootstrap.groovy is a Grails thing, but if you just set the metaClass early on in your applications lifecycle, you should get the same result...
String.metaClass.stripRepeatedWhitespace = { delegate.replaceAll( /( )\1+/, '$1' ) }
In saying all this however, I'd probably just keep the message on a single line

Input string was not in a correct format when parse in multithreading

Does Anybody can explain this:
How does it possible to throw exception when parsing "55.01"? I use multithreading.
--edit--
but... sometimes it works
This realy make me sad ;(
i use .NET 4.0 and VS2010.
--edit 2---
Ok, I made a little progress. When I do not use multithreading everything works perfect. But when I use multithreading (probably)one of a thread throw FormatException in place which is shown in the picture.
It's possible the system is set for some culture that expects a comma as the decimal point.
From http://msdn.microsoft.com/en-us/library/fd84bdyt.aspx:
The s parameter is interpreted using the formatting information in a NumberFormatInfo object that is initialized for the current thread culture. For more information, see CurrentInfo. To parse a string using the formatting information of some other culture, call the Double.Parse(String, IFormatProvider) or Double.Parse(String, NumberStyles, IFormatProvider) method.

VB6 calls to ActiveX component with string parameters yield weird results

I'm currently finishing a piece of software a now gone co-worker started.
The app is coded in VB6 and uses a 3rd party ActiveX component to act upon a 3rd party system. Our solution is basically an integration between their company's software and ours.
The issue I'm having is that there's a method call that fails consistently, even though it's passed perfectly valid parameters on our side (it's a login method). However, when I look at the trace their application offers, I see that instead of the username I specify, it tells me (roughly) "User '⚠⚠⚠' can't login".
I figured it was likely to be an encoding issue as the ⚠ character replacing the characters I give it to log on seem to be there because the characters are unknown, but nothing I did could fix it.
Anyone know of an issue with VB6 communicating with ActiveX components like this? Or anyone have an idea what I could try? I'm at a loss here and if the issue is on their side, it'll be a pain to get it fixed as we don't have their source code.
Thanks in advance.
There are a couple of ways of passing strings. Aside from the obvious one of passing a string as in
DIM u As String
DIM p As String
u = "Username"
p = "Password"
Set objIRC = objRCL.Login(u, p)
there's also the possibility that .Login is expecting pointers to String, in which case code
Set objIRC = objRCL.Login(StrPtr(u), StrPtr(p))

Resources