Unhandled exception, unable to debug - c#

I'm trying to debug my c# application that check MIPS syntax. But its not allowing be to debug it. No matter where I enter my break point it gets ignored, including the first line of the Main() function. Its also throwing me this error.
'add a b c' works fine if i don't call HasValidParams()
'add a b' throws exception in the same situation
neither works when calling HasValidParams()
program.cs
private static void Main(string[] args)
{
var validator = new MipsValidator();
Console.Write("Please enter a MIPS statement: ");
string input = Console.ReadLine();
List<string> arguments = input.Split(new char[0]).ToList();
Response status = validator.IsSyntaxValid(arguments);
//Check syntax
if (status.Success.Equals(true))
{
Response stat = validator.HasValidParams(arguments);
//Check parameters
if (stat.Success.Equals(true))
{
Console.WriteLine(string.Format("'{0}' is a valid mips instruction ", input));
}
else
{
foreach (var reason in stat.Reasons)
{
Console.WriteLine(reason);
}
}
}
else
{
foreach (string reason in status.Reasons)
{
Console.WriteLine(reason);
}
}
}
mips-validator.cs
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace mips_validator.utils
{
public class MipsValidator : IMipsValidator
{
#region Implementation of IMipsValidator
public Response IsSyntaxValid(List<string> args)
{
var response = new Response {Success = true};
var op = (Operator) Enum.Parse(typeof (Operator), args[0]);
switch (op)
{
case Operator.addi:
case Operator.add:
case Operator.beq:
if (args.Count != 4)
{
response.Reasons.Add(string.Format("4 operands required for {0}, {1} parameters provided.",
op, args.Count));
response.Success = false;
}
break;
case Operator.j:
if (args.Count != 2)
{
response.Reasons.Add(string.Format("1 operands required for {1}, {0} parameters provided.",
args.Count, op));
response.Success = false;
}
break;
default:
response.Reasons.Add(string.Format("{0} is an unknown mips operation", op));
response.Success = false;
break;
}
return response;
}
public Response HasValidParams(List<string> parameters)
{
string op1, op2, op3;
var temporary = new Regex(#"/\$t\d+/");
var store = new Regex(#"/\$s\d+/");
var zero = new Regex(#"/\$zero/");
var osReserved = new Regex(#"/\$k0|1/");
var memory = new Regex(#"");
var constant = new Regex(#"/-?\d*/");
var label = new Regex(#"/.*\:/");
Operator operation;
var response = new Response {Success = true};
string opString = parameters[0];
Enum.TryParse(opString.Replace("$", string.Empty), true, out operation);
switch (operation)
{
case Operator.add:
{
op1 = parameters[1];
op2 = parameters[2];
if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
{
response.Reasons.Add(string.Format("{0}: error register expected", op1));
response.Success = false;
}
if (!temporary.IsMatch(op2) && !store.IsMatch(op2) && !zero.IsMatch(op2))
{
response.Reasons.Add(string.Format("{0}: error register expected", op2));
response.Success = false;
}
}
break;
case Operator.addi:
{
op1 = parameters[1];
op2 = parameters[2];
if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
{
response.Reasons.Add(string.Format("{0}: error register expected", op1));
response.Success = false;
}
if (!constant.IsMatch(op2) && !zero.IsMatch(op2))
{
response.Reasons.Add(string.Format("{0}: error constant expected", op2));
response.Success = false;
}
}
break;
case Operator.beq:
{
op1 = parameters[1];
op2 = parameters[2];
op3 = parameters[3];
if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
{
response.Reasons.Add(string.Format("{0}: error register expected", op1));
response.Success = false;
}
if (!temporary.IsMatch(op2) && !store.IsMatch(op2) && !zero.IsMatch(op2))
{
response.Reasons.Add(string.Format("{0}: error register expected", op2));
response.Success = false;
}
if (!label.IsMatch(op3) && !constant.IsMatch(op3))
{
response.Reasons.Add(string.Format("{0}: error label or constant expected", op3));
response.Success = false;
}
}
break;
}
return response;
}
#endregion
}
}
SOLUTION-------
Response.cs(old)
public class Response
{
public List<string> Reasons;
public bool Success = true;
}
Response.cs(current)
public class Response
{
public Response()
{
Reasons = new List<string>();
Success = true;
}
public List<string> Reasons;
public bool Success = true;
}

I can't tell if you're looking for a way to be able to debug your project or if you'd prefer to be told potential issues in your code.
For the latter:
Make sure Response.Reasons is initialized by the constructor of Response (or a field initializer).

You're not showing the Response class, so make sure Reasons is actually set to a collection you can add to and not left to the default, null.
Edit: The below possible cause for a crash was pointed put by #nodakai not to be one at all; turns out an empty char array is a special case to split on whitespace.
*You calculate arguments by doing
List arguments = input.Split(new char[0]).ToList();
...which as far as I can tell does absolutely nothing except put the original string inside a List. You probably want to split on new char[] {' '} instead to split on spaces.*

Check if your breakpoint looks like this:
If it does, your source code differs from the code the assembly was actually compiled with. Make sure your project is built properly (right click on the solution and select "Rebuild") and check your current configuration:
Hope this helps...

Related

C# SAP .net connector not returning the same data as seen in SAP GUI

I have followed the code shown on this site: "https://www.veonconsulting.com/integrating-sap-using-nco/",
I'm trying to get data from the function BAPI_PRODORD_GET_DETAIL, I tested <bapi_prodord_get_detail><number>1000262</number><struct><order_objects><header>X</header><operations>X</operations><components>X</components></order_objects></struct></bapi_prodord_get_detail>, on SAP GUI, I can see data for Header, Components & Operations, but in the code, I 'm not getting the same response, could you please help.
Code:
public bool testConnection()
{
bool state = false;
string rfcRequest = "<RFC_READ_TABLE><QUERY_TABLE>MARD</QUERY_TABLE><DELIMITER>*"
+ "</DELIMITER><ROWSKIPS>0</ROWSKIPS><ROWCOUNT>0</ROWCOUNT><TABLE><OPTIONS><ROW>"
+ "<TEXT>MATNR IN (</TEXT></ROW><ROW><TEXT>'testConnection'</TEXT></ROW><ROW>"
+ "<TEXT>)</TEXT></ROW></OPTIONS></TABLE></RFC_READ_TABLE>";
Utils.RfcClient client = new Utils.RfcClient();
try
{
XElement response = client.PullRequestToSAPrfc(rfcRequest);
state = true;
}
catch (RfcLogonException ex)
{
Console.Write("Logon Failed");
}
catch (RfcInvalidStateException ex)
{
Console.Write("RFC Failed");
}
catch (RfcBaseException ex)
{
Console.WriteLine("communication error" + ex.Message);
}
catch (Exception ex)
{
Console.Write("Connection error");
}
finally
{
//client.disconnectDestination();
}
return state;
}
public bool testConnection()
{
bool state = false;
string rfcRequest = "<bapi_prodord_get_detail><number>1000262</number><struct>"
+ "<order_objects><header>X</header><operations>X</operations><components>X"
+ "</components></order_objects></struct></bapi_prodord_get_detail>";
Utils.RfcClient client = new Utils.RfcClient();
try
{
XElement response = client.PullRequestToSAPrfc(rfcRequest);
state = true;
}
catch (RfcLogonException ex)
{
Console.Write("Logon Failed");
}
catch (RfcInvalidStateException ex)
{
Console.Write("RFC Failed");
}
catch (RfcBaseException ex)
{
Console.WriteLine("communication error" + ex.Message);
}
catch (Exception ex)
{
Console.Write("Connection error");
}
finally
{
//client.disconnectDestination();
}
return state;
}
public XElement PullRequestToSAPrfc(string XMLRequest)
{
IRfcFunction requestFn;
requestFn = PrepareRfcFunctionFromXML(XElement.Parse(XMLRequest));
RfcSessionManager.BeginContext(_ECCsystem);
requestFn.Invoke(_ECCsystem);
RfcSessionManager.EndContext(_ECCsystem);
XElement XMLResponse = PrepareXMLFromrfc(requestFn);
return XMLResponse;
}
public IRfcFunction PrepareRfcFunctionFromXML(XElement xmlFunction)
{
RfcRepository repo = _ECCsystem.Repository;
IRfcFunction RfcFunction = repo.CreateFunction(xmlFunction.Name.ToString());
foreach (XElement xelement in xmlFunction.Elements())
{
if (xelement.Name.ToString().Equals("TABLE"))
{
if (NotProcessSpecialTable(xelement))
continue;
IRfcTable options = RfcFunction.GetTable(xelement.Descendants().First().Name.ToString());
foreach (XElement row in xelement.Elements().First().Elements())
{
options.Append();
foreach (XElement rowElement in row.Elements())
{
string elementName = rowElement.Name.ToString();
RfcElementMetadata elementMeta = options.GetElementMetadata(elementName);
var elementValue = getValueAsMetadata(ref elementMeta, rowElement.Value);
if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; }
options.SetValue(elementName, elementValue);
}
}
}
else if (xelement.Name.ToString().Equals("STRUCT"))
{
IRfcStructure options = RfcFunction.GetStructure(xelement.Descendants().First().Name.ToString());
foreach (XElement structElement in xelement.Elements().First().Elements())
{
string elementName = structElement.Name.ToString();
RfcElementMetadata elementMeta = options.GetElementMetadata(elementName);
var elementValue = getValueAsMetadata(ref elementMeta, structElement.Value);
if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; }
options.SetValue(elementName, elementValue);
}
}
else
{
string elementName = xelement.Name.ToString();
RfcElementMetadata elementMeta = RfcFunction.GetElementMetadata(elementName);
var elementValue = getValueAsMetadata(ref elementMeta, xelement.Value);
if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; }
RfcFunction.SetValue(elementName, elementValue);
}
}
return RfcFunction;
}
public XElement PrepareXMLFromrfc(IRfcFunction rfcFunction)
{
var XMLRoot = new XElement(rfcFunction.Metadata.Name);
for (int functionIndex = 0; functionIndex < rfcFunction.ElementCount; functionIndex++)
{
var functionMatadata = rfcFunction.GetElementMetadata(functionIndex);
if (functionMatadata.DataType == RfcDataType.TABLE)
{
var rfcTable = rfcFunction.GetTable(functionMatadata.Name);
var XMLTable = new XElement(functionMatadata.Name);
foreach (IRfcStructure rfcStracture in rfcTable)
{
XElement XMLRow = new XElement("ROW");
for (int i = 0; i < rfcStracture.ElementCount; i++)
{
RfcElementMetadata rfcElementMetadata = rfcStracture.GetElementMetadata(i);
if (rfcElementMetadata.DataType == RfcDataType.BCD)
{ XMLRow.Add(new XElement(rfcElementMetadata.Name, rfcStracture.GetString(rfcElementMetadata.Name))); }
else
{
XMLRow.Add(new XElement(rfcElementMetadata.Name, rfcStracture.GetString(rfcElementMetadata.Name)));
}
}
XMLTable.Add(XMLRow);
}
XMLRoot.Add(XMLTable);
}
else if (functionMatadata.DataType == RfcDataType.STRUCTURE)
{
var rfcStructure = rfcFunction.GetStructure(functionMatadata.Name);
XElement XMLRow = new XElement(functionMatadata.Name);
for (int elementIndex = 0; elementIndex < rfcStructure.ElementCount; elementIndex++)
{
RfcElementMetadata eleMeta = rfcStructure.GetElementMetadata(elementIndex);
XMLRow.Add(new XElement(eleMeta.Name, rfcStructure.GetString(eleMeta.Name)));
}
XMLRoot.Add(XMLRow);
}
else
{
RfcElementMetadata rfcElement = rfcFunction.GetElementMetadata(functionIndex);
XMLRoot.Add(new XElement(rfcElement.Name, rfcFunction.GetString(rfcElement.Name)));
}
}
return XMLRoot;
}
# Below function is used for the data types.
private object getValueAsMetadata(ref RfcElementMetadata elementMeta, string value)
{
switch (elementMeta.DataType)
{
case RfcDataType.BCD:
return value;
case RfcDataType.NUM:
if (value.Contains("."))
{
int elementValue;
int.TryParse(value, out elementValue);
return elementValue;
}
else
{
return Convert.ToInt32(value);
}
case RfcDataType.INT1:
return Convert.ToInt32(value);
case RfcDataType.INT2:
return Convert.ToInt32(value);
case RfcDataType.INT4:
return Convert.ToInt32(value);
case RfcDataType.INT8:
return Convert.ToInt64(value);
case RfcDataType.CHAR:
return value;
case RfcDataType.DATE:
return DateTime.ParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture);
default:
return string.Empty;
}
}
You are confronted to the classic issue of external and internal values in SAP.
Your screenshot shows the ABAP Function Module Test screen in SAP system. When you enter a value in the screen, it may be transformed internally before calling the function module.
These are called the external and internal formats. "External" is what is shown in the User Interface (typed or displayed), "internal" is the value written to the database.
For instance, imagine a database object whose primary key is a GUID, this is the object key in internal format, but in the user interface this object is always referred or shown by its name (candidate key).
In your precise case, when a Production Order is a number, the internal format always contains leading zeroes on 12 digits, and the external format does not display them. In the Function Module Test screen, if you enter this external value:
1000262
it's converted to the following internal value and the BAPI is called with it:
000001000262
Generally speaking, when you call any function module from another program, you must indicate the internal value, because there's no user interface implied between the two.
i.e., use this XML in your method testConnection:
string rfcRequest = "<bapi_prodord_get_detail><number>000001000262</number><struct>"
+ "<order_objects><header>X</header><operations>X</operations><components>X"
+ "</components></order_objects></struct></bapi_prodord_get_detail>";
See also this answer about external and internal formats: Converting MATNR via conversion exit fails for custom table
If you would like to do the required field conversions programmatically, which are explained in Sandra Rossi's answer, you may use the RFMs BAPI_CONVERSION_EXT2INT, BAPI_CONVERSION_EXT2INT1, BAPI_CONVERSION_INT2EXT and BAPI_CONVERSION_INT2EXT1 for doing so.
However, every additional RFC call has of course a negative impact on the performance.
Besides, SAP Note 206068 is a good resource for an explanation of some RFC BAPI pitfalls which you also stepped in.

How to check each line from an website/php file and Enable/disable Components C#

I try to make a function that allows me to enable and disable multiple components from a server. Preferably from one php file.
Example:
Website Side
AdminPanelEnabled=True
UserIPPanelEnabled=False
GuestPanelEnabled=True
(On the website there is no other code just what you see above)
If it is set to true on the server/website it will enable the component or if set to false it will disable the component.
Tries and fails
Checker = new WebClient().DownloadString("URL/Checker.php");
if (Checker == "AdminPanelEnabled=True")
{
AdminPanel.Enabled = true;
}
else
{
AdminPanel.Enabled = false;
}
if (Checker == "UserIPPanelEnabled=True")
{
UserIPPanel.Enabled = true;
}
else
{
UserIPPanel.Enabled = false;
}
if (Checker == "GuestPanelEnabled=True")
{
GuestPanel.Enabled = true;
}
else
{
GuestPanel.Enabled = false;
}
The file you provided consists of multiple lines, so you have to separate the lines first and then check every single line. So, maybe something like this works?
string[] lines = new WebClient().DownloadString("Checker.php")
.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
foreach (var line in lines)
{
string[] config = line.Split('=');
if (config[0] == "AdminPanelEnabled")
{
AdminPanel.Enabled = bool.Parse(config[1]);
}
else if (config[0] == "UserIPPanelEnabled")
{
UserIPPanel.Enabled = bool.Parse(config[1]);
}
else if (config[0] == "GuestPanelEnabled")
{
GuestPanel.Enabled = bool.Parse(config[1]);
}
}

Convert boolean method to asynchronous one

I have method that execute Email sender like this:
public void SendIt()
{
if (_Error && !_Override) return;
var html = new StringBuilder();
html.Append(HTMLHeader());
html.Append(Body);
html.Append(HTMLClose());
IsHTML = true;
SendAsync = true;
Body = html.ToString();
Send();
}
I want to change this method to asynchronous method so I do:
public async void SendIt()
{
await Send();
}
But I get
'bool' does not contain a definition for 'GetAwaiter' and no
extension method 'GetAwaiter' accepting a first argument of type
'bool' could be found (are you missing a using directive or an
assembly reference?)
Send method:
public bool Send()
{
var MailFrom = new MailAddress(_From, _From);
_Email.From = MailFrom;
if (_To.IndexOf(';') > -1)
{
string[] tolist = _To.Split(';');
foreach (string toAddress in tolist)
_Email.To.Add(new MailAddress(toAddress.Trim()));
}
else _Email.To.Add(new MailAddress(_To.Trim()));
if (_CC != null && _CC.Length > 5)
{
if (_CC.IndexOf(';') > -1)
{
string[] Cclist = _CC.Split(';');
foreach (string ccAddress in Cclist)
_Email.CC.Add(new MailAddress(ccAddress.Trim()));
}
else _Email.CC.Add(new MailAddress(_CC.Trim()));
}
if (_BCC != null && _BCC.Length > 5)
{
if (_BCC.IndexOf(';') > -1)
{
string[] Bcclist = _BCC.Split(';');
foreach (string bccAddress in Bcclist)
_Email.Bcc.Add(new MailAddress(bccAddress.Trim()));
}
else _Email.Bcc.Add(new MailAddress(_BCC.Trim()));
}
_Email.Subject = _Subject;
_Email.IsBodyHtml = _isHTML;
_Email.Body = _Body;
_Email.BodyEncoding = Encoding.UTF8;
// Smtp Client
var Connection = new SmtpClient(_Server, _Port)
{
DeliveryMethod = SmtpDeliveryMethod.Network,
Timeout = 60000
};
if (_UserName.Length > 1 && _Password.Length > 1 && _Domain.Length > 1)
Connection.Credentials = new NetworkCredential(_UserName, _Password, _Domain);
else if (_UserName.Length > 1 && _Password.Length > 1)
Connection.Credentials = new NetworkCredential(_UserName, _Password);
bool bFlag = true;
Connection.EnableSsl = _SSL;
try
{
Connection.Send(_Email);
}
catch (SmtpFailedRecipientsException ex)
{
var msg = new StringBuilder();
for (int k = 0; k < ex.InnerExceptions.Length; k++)
{
var StatusCode = ex.InnerExceptions[k].StatusCode;
if (StatusCode == SmtpStatusCode.MailboxUnavailable || StatusCode == SmtpStatusCode.MailboxBusy)
{
ErrMsg = "Failed to deliver message to " + ex.FailedRecipient[k].ToString();
try
{
if (_TryAgainOnFailure)
{
System.Threading.Thread.Sleep(_TryAgainDelayTime);
// Send the message
string sTemp = "";
if (_SendAsync)
Connection.SendAsync(_Email, sTemp);
else Connection.Send(_Email);
}
_ErrMsg = string.Empty;
}
catch (SmtpException sex)
{
_ErrMsg = sex.Message;
_Error = true;
}
}
}
}
catch (SmtpException sex)
{
string dat = sex.Data.ToString();
_ErrMsg = sex.Message;
_Error = true;
}
return bFlag;
}
How can I convert this boolean method to async method?
I try something like:
public async bool Send() {
But I get:
The return type of an async method must be void, Task or Task
What should I do to solve this? Regards
The return type of an async method must be void, Task or Task<T>
Like the error states you have to change the return type to Task<bool>. You will then have to change the method itself so it awaits a Task or returns a Task.
public async Task<Boolean> SendAsync() { // renamed your method with suffix Async
// ... code unchanged
try {
await Connection.SendMailAsync(this._Email);
}
// ... code unchanged
}
As for your other methods that call this they should also return type Task unless they are event driven like a windows form or wpf click event.
See also SendMailAsync
You must return Task<T> if you use async method.
Correct definition for your method is:
public async Task<Boolean> Send()
Also you need to use await keyword inside your method.
You can write Send method using SendMailAsync.
Example is here:
public async Task Send(string to, string subject, string body)
{
var message = new MailMessage
{
Subject = subject,
Body = body
};
message.To.Add(to);
using (var smtpClient = new SmtpClient())
{
await smtpClient.SendMailAsync(message);
}
}
More details you can review here.
Also please review details about async/await programming here.

False positive with rule S2538 in nested while-switch-switch construction

We are getting false positives while using rule S2538 in the following code
EventLogLevel[] eventLevels = null;
bool reachedEnd = false;
while(!reachedEnd && jsonReader.Read())
{
switch(jsonReader.TokenType)
{
case JsonToken.PropertyName:
string propertyName = jsonReader.Value.ToString();
switch(propertyName)
{
case nameof(EventLevels):
eventLevels = EventSettingsJson.ParseEventLogLevelsArray(nameof(EventLevels), jsonReader);
break;
default:
throw new JsonParserException($"Invalid property: {propertyName}");
}
break;
case JsonToken.EndObject:
reachedEnd = true;
break;
default:
throw new JsonParserException($"Unexpected Token Type while parsing json properties. TokenType: {jsonReader.TokenType}");
}
}
if(eventLevels != null)
{
return new EventLogCollectionSettings(eventLogName, eventLevels);
}
The last if (eventLevels != null) shows the warning with the message:
[Change this condition so that it does not always evaluate to
"false"].
I couldn't create a testcase to reproduce it.
We know about this limitation in our data flow analysis engine. It's related to this ticket: https://jira.sonarsource.com/browse/SLVS-1091. We have no fix for it yet.

Trapping Adwords errors in JScript when called from ClearScript?

Context: VS2015 Community; C#; ClearScript.V8.5.4.5; Google.AdWords.18.25.0
For background on this posting, see an earlier posting (Kudos, by the way, to #BitCortex for solving the first conundrum.)
I now have a working Adwords mutation scripted in JScript via ClearScript and C#. The challenge now is to handle errors.
In the following chunk of code, I'm creating a new BudgetOrder:
var order = new BudgetOrder();
order.billingAccountId = acct.id;
order.startDateTime = "20160801 000000 Australia/Perth";
order.endDateTime = "20160831 235959 Australia/Perth";
var amt = new Money();
amt.microAmount = 10000000;
order.spendingLimit = amt;
var boo = new BudgetOrderOperation();
boo.operator = Operator.ADD;
boo.operand = order;
var mutations = ToTypedArray(BudgetOrderOperation, [boo]);
var response;
try {
response = bos.mutate(mutations);
Console.WriteLine(response.value[0].billingAccountId);
Console.WriteLine(response.value[0].id);
Console.WriteLine(response.value[0].lastRequest.status.ToString());
} catch (exc) {
Console.WriteLine(exc.message);
}
...
function ToTypedArray(typ, arr) {
var T;
if ("string" === typeof typ) {
T = host.type(typ);
} else {
T = typ;
}
var a = host.newArr(T, arr.length);
for (var i = 0; i < arr.length; i++) {
a.SetValue(arr[i], i);
}
return a;
}
The problem I'm having at the moment is that if there is an error, exc doesn't have anything useful in it apart from
exc
{...}
description: ""
message: ""
name: "Error"
number: -2146190593
for example, and response is undefined.
The usual data that would be available in a BudgetOrderReturnValue running natively in C# is not stored anywhere I can see.
I did try casting the result of the mutate using
response = host.cast(BudgetOrderReturnValue,bos.mutate(mutations));
but when the error occurs, response is still set as undefined.
I've been able to capture the XML for the mutate having specified
<add name="AdsClientLibs.DetailedRequestLogs" value="All" />
in App.config which gives me a detailed_logs.log in C:\Logs\Adwords. Thus when an error occurs I've been able to go back to that log and see what the error was, e.g.
<detail>
<ns2:ApiExceptionFault xmlns="https://adwords.google.com/api/adwords/cm/v201603" xmlns:ns2="https://adwords.google.com/api/adwords/billing/v201603">
<message>[BudgetOrderError.INVALID_BUDGET_DATE_RANGE # operations[0].operand.startDateTime.endDateTime; trigger:'Overlapping budget found']</message>
<ApplicationException.Type>ApiException</ApplicationException.Type>
<errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:BudgetOrderError">
<fieldPath>operations[0].operand.startDateTime.endDateTime</fieldPath>
<trigger>Overlapping budget found</trigger>
<errorString>BudgetOrderError.INVALID_BUDGET_DATE_RANGE</errorString>
<ApiError.Type>BudgetOrderError</ApiError.Type>
<ns2:reason>INVALID_BUDGET_DATE_RANGE</ns2:reason>
</errors>
</ns2:ApiExceptionFault>
</detail>
However, none of that data seems to be available to the script.
Ideas, anyone?
LATER
var response;
var hostException;
var succeeded = host.tryCatch(
function () {
response = bos.mutate(mutations);
return true;
},
function (exception) {
hostException = exception;
return false;
});
if (succeeded) {
// process response
Console.WriteLine(response.value[0].billingAccountId);
Console.WriteLine(response.value[0].id);
Console.WriteLine(response.value[0].lastRequest.status.ToString());
} else {
// handle host exception
if (host.isType(BudgetOrderError, hostException)) {
Console.WriteLine("BudgetOrderException");
} else if (host.isType(ClientTermsError, hostException)) {
Console.WriteLine("ClientTermsError");
}
//...
}
Unfortunately, this doesn't work. The bos.mutate line causes the script to crash with an uncaught error.
NEXT DAY
The output from the EXE running the script:
Exception has been thrown by the target of an invocation.
at JScript global code (Script Document [temp]:149:0) -> var succeeded = host.tryCatch(
function () {
response = bos.mutate(mutations);
return true;
},
function (exception) {
hostException = exception;
return false;
})
The C# code
string script = File.ReadAllText(scriptSpec);
try
{
answer = JSengine.Evaluate(script);
}
catch (ScriptEngineException see)
{
Console.WriteLine(see.ErrorDetails);
ScriptEngineException next = see.InnerException as ScriptEngineException;
while (next != null)
{
Console.WriteLine(next.ErrorDetails);
next = next.InnerException as ScriptEngineException;
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
The JScript code as above. So the ClearScript engine appears not to be doing well with tryCatch.
A COUPLE OF DAYS LATER
I've learned one thing at least out of this: I don't need to put
WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableJITDebugging
into my C# code when instantiating the JScriptEngine object. If there's a debugger; statement in the script, I'll get prompted to start a debugging session.
But back to the script
debugger;
var CFG = new Config(Path.Combine(Settings.Item("EXEPath"), "mutator2.cfg"));
var config = new AdWordsAppConfig();
config.DeveloperToken = CFG.Retrieve("DeveloperToken");
config.UserAgent = CFG.Retrieve("UserAgent");
config.ClientCustomerId = CFG.Retrieve("CustomerID");
config.RetryCount = 10;
var user = new AdWordsUser(config);
user.OAuthProvider.ClientId = CFG.Retrieve("ClientId");
user.OAuthProvider.ClientSecret = CFG.Retrieve("ClientSecret");
//user.OAuthProvider.AccessToken = CFG.Retrieve("AccessToken");
user.Config.OAuth2RefreshToken = CFG.Retrieve("OAuth2RefreshToken");
try {
user.OAuthProvider.RefreshAccessToken();
} catch (ex) {
Console.WriteLine("RefreshAccessToken failed.");
Environment.Exit(1);
}
var bos = user.GetService(AdWordsService.v201603.BudgetOrderService);
bos = host.cast(BudgetOrderService, bos);
//bos.RequestHeader.clientCustomerId = config.ClientCustomerId;
//bos.RequestHeader.developerToken = config.DeveloperToken;
//bos.RequestHeader.userAgent = config.UserAgent;
bas = bos.getBillingAccounts();
var order = new BudgetOrder();
order.billingAccountId = CFG.Retrieve("BillingID");
order.startDateTime = "20160801 000000 Australia/Perth";
order.endDateTime = "20160830 000000 Australia/Perth";
var amt = new Money();
amt.microAmount = 10000000;
order.spendingLimit = amt;
var boo = new BudgetOrderOperation();
boo.operator = Operator.ADD;
boo.operand = order;
var mutations = ToTypedArray(BudgetOrderOperation, [boo]);
// bos.RequestHeader.validateOnly = true;
var response;
var hostException;
var succeeded = host.tryCatch(
function () {
response = bos.mutate(mutations);
},
function (exception) {
hostException = exception;
return true;
});
if (succeeded) {
// process response
Console.WriteLine(response.value[0].billingAccountId);
Console.WriteLine(response.value[0].id);
Console.WriteLine(response.value[0].lastRequest.status.ToString());
} else {
// handle host exception
if (host.isType(BudgetOrderError, hostException)) {
Console.WriteLine("BudgetOrderException");
} else if (host.isType(ClientTermsError, hostException)) {
Console.WriteLine("ClientTermsError");
}
//...
}
function qq(v, d) {
if (null === v) {
return "undefined" === typeof d ? "" : d;
} else {
return v;
}
}
function ToTypedArray(typ, arr) {
var T;
if ("string" === typeof typ) {
T = host.type(typ);
} else {
T = typ;
}
var a = host.newArr(T, arr.length);
for (var i = 0; i < arr.length; i++) {
a.SetValue(arr[i], i);
}
return a;
}
The first time through, it works fine. The second time through, with the dates unchanged, throws an AdWords error (date range already taken) which causes the JScriptEngine to throw an unhandled exception error. I get prompted to start a debugging session which on launch shows a dialog containing
Unhandled exception at line 52, column 2 in JScript - script block
0x8013baff - unknown exception
and the highlight on the line response = bos.mutate(mutations);. And this happens whether I've got a debugger; statement or not.
So I'm giving up on the scripting of AdWords using ClearScript. Maybe I should file this as a bug with the folk at ClearScript.
JScript has limited support for host exception handling, but you could try HostFunctions.tryCatch:
var hostException;
var succeeded = host.tryCatch(
function() {
response = bos.mutate(mutations);
},
function(exception) {
hostException = exception;
return true;
}
);
if (succeeded) {
// process response
...
}
else {
// handle host exception
if (host.isType(BudgetOrderError, hostException)) { ... }
else if (host.isType(ClientTermsError, hostException)) { ... }
...
}
Obviously for this to work you must expose the host exception types (BudgetOrderError et al) via ScriptEngine.AddHostType.

Categories