public void Catch(BuildException be) { bool propertyExists = false; string originalPropertyValue = null; if (Property != null) { propertyExists = Project.Properties.Contains(Property); originalPropertyValue = Project.Properties[Property]; Project.Properties[Property] = be.RawMessage; } try { Execute(); } finally { if (Property != null) { if (!propertyExists) { // if the property did not exist, then remove it again Project.Properties.Remove(Property); } else { // restore original value Project.Properties[Property] = originalPropertyValue; } } } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Sets a single environment variable. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="value">The value.</param> /// ------------------------------------------------------------------------------------ private void SetSingleEnvironmentVariable(string variableName, string value) { Log(Level.Verbose, "Setting environment variable {0} to {1}", variableName, value); // expand any env vars in value string expandedValue = Environment.ExpandEnvironmentVariables(value); bool fSuccess = SetEnvironmentVariable(variableName, expandedValue); if (fSuccess && m_fGlobal) { m_VariablesToSet.Add(string.Format("\"{0}={1}\"", variableName, expandedValue)); } if (!fSuccess) { int nError = Marshal.GetLastWin32Error(); string msg = string.Format("Error {0} setting environment variable {1}", nError, variableName); if (FailOnError) { BuildException ex = new BuildException(msg, Location); ex.Data.Add("Name", variableName); throw ex; } Log(Level.Info, msg); } }
private void WriteErrorNode(Exception exception) { // this method assumes that a synchronization // lock on _xmlWriter is already held if (exception == null) { // build success return; } else { BuildException buildException = null; if (typeof(BuildException).IsAssignableFrom(exception.GetType())) { buildException = (BuildException)exception; } if (buildException != null) { // start build error node _xmlWriter.WriteStartElement("builderror"); } else { // start build error node _xmlWriter.WriteStartElement("internalerror"); } // write exception type _xmlWriter.WriteElementString("type", exception.GetType().FullName); // write location for build exceptions if (buildException != null) { // write raw exception message if (buildException.RawMessage != null) { _xmlWriter.WriteStartElement("message"); _xmlWriter.WriteCData(StripCData(buildException.RawMessage)); _xmlWriter.WriteEndElement(); } if (buildException.Location != null) { if (!StringUtils.IsNullOrEmpty(buildException.Location.ToString())) { _xmlWriter.WriteStartElement("location"); _xmlWriter.WriteElementString("filename", buildException.Location.FileName); _xmlWriter.WriteElementString("linenumber", buildException.Location.LineNumber.ToString(CultureInfo.InvariantCulture)); _xmlWriter.WriteElementString("columnnumber", buildException.Location.ColumnNumber.ToString(CultureInfo.InvariantCulture)); _xmlWriter.WriteEndElement(); } } } else { // write exception message if (exception.Message != null) { _xmlWriter.WriteStartElement("message"); _xmlWriter.WriteCData(StripCData(exception.Message)); _xmlWriter.WriteEndElement(); } } // write stacktrace of exception to build log _xmlWriter.WriteStartElement("stacktrace"); _xmlWriter.WriteCData(exception.StackTrace); _xmlWriter.WriteEndElement(); // write information about inner exception WriteErrorNode(exception.InnerException); // close failure node _xmlWriter.WriteEndElement(); } }
protected override void ExecuteTask() { if (string.IsNullOrWhiteSpace(this.test) && !this.iterations.HasValue && !this.untilSuccess) throw new BuildException("Property, Iterations or UntilSuccess (or a combination) must be defined."); for (int executioncount = 0; this.Loop(executioncount); executioncount++) { // Sleep if a sleep time has been specified. // but not in the first iteration. if (this.sleep != 0 && executioncount != 0) { this.Log(Level.Info, string.Format("Sleeping for {0:0.0} seconds.", this.sleep / 1000.0)); Thread.Sleep(this.sleep); } // store the iterator value so it can be accessed by the script if (!string.IsNullOrWhiteSpace(this.iterator)) this.Project.Properties[this.iterator] = (executioncount + 1).ToString(); if (!this.untilSuccess) { this.ExecuteChildTasks(); // If not "until success", then any exception is a failure condition. } else { try { this.ExecuteChildTasks(); return; // If "until success", then we're successful at this point and can stop. } catch (BuildException ex) { this.storedException = ex; } } } }