C# powershell ErrorDetail usage - c#

I have a custom powershell command derived from PSCmdlet and on error I want to provide my own error message. As per MSDN ErrorDetails propery on ErrorRecord does that :
Provides additional error information for an error record, such as a
more detailed replacement error message.
How ever I am not able to get it working, I have DisplayStrings.resx, in this I have stored my custom error message with the ID InvalidOrNoSite, but when I execute the following code, it shows the same old style error and not my custom error.
ErrorRecord err = new ErrorRecord(new ArgumentException(), "", ErrorCategory.InvalidArgument, this);
err.ErrorDetails = new ErrorDetails(this, "Resources.DisplayStrings.ResourceManager", "InvalidOrNoSite", null);
ThrowTerminatingError(err);
Any examples on how to use ErrorDetails?

The customisation of ErrorDetail works if you change your error in a non-terminating error.
This behaviour is not explained in microsoft documentation [ or at least I have not found it :) ] but in a book I've buy some year ago ( Wrox - Professional Windows PowerShell Programming - February 2008 ) at page 97 is reported that in terminating error doesn't work.
My workaround is to set a 'flag' variable to true in the catch and after test this variable and do or not do some or all following code.

Related

Why different error codes for same Exception?

I am doing one synchronization example. I got two exceptions which are having same details about exception. But with the error codes.
And both exceptions have the same Inner Exceptions
What is means? What Error Codes will tell us more than exceptions?
How to get the details about my error code.
Thanks in Advance
Microsoft.Synchronization.SyncException: A storage engine operation failed with error code 25051 (HRESULT = 0x80004005, Source IID = {0FB15084-AF41-11CE-BD2B-204C4F4F5020}, Parameters=(0, 0, 0, , , , )).
Microsoft.Synchronization.SyncException: A storage engine operation failed with error code 25009 (HRESULT = 0x80004005, Source IID = {0FB15084-AF41-11CE-BD2B-204C4F4F5020}, Parameters=(0, 0, 0, , , , )).
From the wording of the message it looks like these are SQL CE errors. If so, have a look here for the meaning of the actual error codes. For .NET code that wraps native code, it is not uncommon that one managed exception, SyncException here, is used with multiple error codes in addition (most prominent example Win32Exception).
From the documentation there seems to be no apparent way to get this numeric code.
However, whether you can really react differently in your code, depending on one error code or the other, is questionable anyway. Log the error with all details
and be done with it.
Finally got cleaned after reading the documentation

Application crashes on element reader (pdftron)

I have an issue with pdftron, where opening a certain file, will cause our application to crash with following error:
An unhandled exception of type 'pdftron.Common.PDFNetException' occurred in PDFNet.dll
Additional information: Exception:
Message: Missing resource
Conditional expression: res
Filename : ContentResources.hpp
Function : trn::PDF::ContentResources::GetResource
Linenumber : 26
In our code: it's in the following line that the error occurs:
while ((element = elReader.Next()) != null)
When doing try/catch, we see that the only thing missing from the page is the text that's written diagonally on that page. Does this have anything to do with a missing font maybe ? Don't mind the cursor in the picture, it doesn't know where to go with the text missing.
I can send the pdf file on request.
PDF File
If you are not on the latest version of PDFNet, 6.7.1, then I would first try against that, as the issue might have been resolved already.
Otherwise, since the issue is document specific, you would need to provide that, by either sharing here, or sending to pdftron support.

Get error message from Command Line Parser Library

I am using Command Line Parser Library for C# console application.
How I can retreive error messages if error occurs?
The use case is that this console app will be called from some another app and I want to provide to that app error message if some error occurs.
Here is a code snippet for parsing:
var options = new Options();
if (!CommandLine.Parser.Default.ParseArguments(args, options))
{
string errorMessage = ExctractParsingErrors();
GenerateErrorInvalidArguments(errorMessage);
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
}
Inside function ExctractParsingErrors() I have to extract parsing error. Example output of that function could be "-i/--input required option is missing."
Any idea how I could manage to extract parsing errors?
There is an additional overload that allows you to pass a TextWriter helpWriter, according to the documentation of the component you linked.

NullPointerException with MR2 in windows

I have installed Hadoop 2.3.0 in windows and able to execute MR jobs successfully. But while trying with streaming sample in C# [with HadoopSDK's .Net assemblies] the app ends with the following exception
14/05/16 18:21:06 INFO mapreduce.Job: Task Id : attempt_1400239892040_0003_r_000000_0, Status : FAILED
Error: java.lang.NullPointerException
at org.apache.hadoop.mapred.Task.getFsStatistics(Task.java:347)
at org.apache.hadoop.mapred.ReduceTask$OldTrackingRecordWriter.<init>(ReduceTask.java:478)
at org.apache.hadoop.mapred.ReduceTask.runOldReducer(ReduceTask.java:414)
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:392)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:168)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1548)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)
Update:
I was able to drill down the problem and found that the exception raised in the following line
matchedStats = getFsStatistics(FileOutputFormat.getOutputPath(job), job);
at org.apache.hadoop.mapred.ReduceTask$OldTrackingRecordWriter.<init>(ReduceTask.java:478)
In the above, the result of 'FileOutputFormat.getOutputPath(job)' returns null, which throws the null pointer exception. Below are the codes for the getOutputPath() function.
public static final String OUTDIR = "mapreduce.output.fileoutputformat.outputdir";
public static Path getOutputPath(JobConf conf) {
String name = conf.get(org.apache.hadoop.mapreduce.lib.output.
FileOutputFormat.OUTDIR);
return name == null ? null: new Path(name);
}
So Is it needed to set value for property "mapreduce.output.fileoutputformat.outputdir" anywhere in the configuration files to get this issue resolved?
Thanks
The problem is that the hadoop services have been started from a different user [SYSTEM in my case] and the mapreduce sample was submitted from my local user. So this makes the issue by returning the FileSystem statistics [for the local user] as Null.
Once i started Hadoop from my local user, the above issue gets resolved.

Copy and read files from dvd and CD C#

I am trying to use the following code in my project. http://www.codeproject.com/KB/miscctrl/imapi2.aspx
However, When I run the application and click on "Detect Media" it says "Media not supported".
Can someone please help me with this issue. Why does it say Media not supported?
Thank you,
Divya.
Referring to Eric's source code for the application, this text comes from the buttonDetectMedia_Click method in the MainForm class:
discFormatData = new MsftDiscFormat2Data();
if (!discFormatData.IsCurrentMediaSupported(discRecorder))
{
labelMediaType.Text = "Media not supported!";
_totalDiscSize = 0;
return;
}
So, the call to IsCurrentMediaSupported is failing. This is actually a COM Interop call to IDiscFormat2::IsCurrentMediaSupported. The MSDN documentation does mention some other possible HRESULT values, though I'd expect that if they occurred, a COMException would be thrown. The sample code does catch this exception, in which case a message box is displayed - that's not the case here though.
When I ran the sample, I got the same "Media not supported!" error. I have a DVD burner, but there is no disc in the drive (don't have any blank discs with me at the moment!), so that appears to be one answer to why you'd get that message. I'd guess if the media in the drive was not writable or incompatible with your burner, you'd also get that message.

Categories