Invoke-Sqlcmd2 do not terminate on error - sql

Is there any way to make the function Invoke-Sqlcmd2 not terminate the rest of the script on failure?
I want the rest of my script to continue to run even if this command fails.
I've dug around the code and my thought is it has something to do with the -ErrorAction parameter.
Below is line 488 of the script:
Catch # For other exception
{
Write-Verbose "Capture Other Error"
$Err = $_
if ($PSBoundParameters.Verbose) {Write-Verbose "Other Error: $Err"}
switch ($ErrorActionPreference.tostring())
{
{'SilentlyContinue','Ignore' -contains $_} {}
'Stop' { Throw $Err} # Removing this line doesn't work
'Continue' { Throw $Err}
Default { Throw $Err}
}
}
Any ideas on how to get this command to 'non-terminate'?
Solution:
Thanks for the info. I ended up wrapping the error handling section of invoke-sqlcmd2 in a try-catch trap.

Putting the Invoke-SQLCMD2 inside a try catch block and maybe log the error thrown.
That way you will handle the error and the script will go on.

Related

how to handle error 500 when requesting a distant server with guzzle?

i am requesting a webservice using :
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
try {
$client = new Client();
$response = $client->request('GET', $url); //it crashes at this line
$content = json_decode($response->getBody(), true);
}
catch (ConnectException $e) {
\Drupal::logger('amu_hal')->error('incorrect_url'.$url);
}
today the distant server return a error 500.
How can i modify my code not to crash my site when it happens?
I assume that by distant server you mean a server that takes a long time to connect. You can specify a timeout for the request.
Or perhaps the server returned error 500 and it fails during json_decode? You can check the status code returned by the request.
Or even perhaps the code is failing the line that you indicate but the exception ConnectException is not being caught? Try using Exception as a catch-all to debug this situation.
Instead of using Guzzle directly, I recommend that you use the Drupal wrapper (which uses Guzzle under the hood).
$client = Drupal::httpClient();
$request = $client->get($uri, ['connect_timeout' => 5]);
if ($request->getStatusCode() === 200) {
echo 'Connection Success';
} else {
echo sprintf('Error %d occurred', $request->getStatusCode());
}

catching the assertion failure message before handling it

I know how to raise an exception and how to handle it , but suppose I have this method:
method1:arg
AssertionFailure signal:'rescue error comment'.
I want to catch this exception and create new Assertion failure exception with another format.However I need to get that message ("rescue error comment") and use it in my new exception.. and this is where I don't know what to do...
so how do I get that message before the handling the exeption by using
on: AssertionFailure do:
Either use
on: AssertionFailure do: [ :e | NewAssertionFailure signal: e messageText ]
or define method as
method1:arg
NewAssertionFailure signal: 'rescue error comment'

How hide PDOException with zend framework 2 on production environment?

We use a zend framework 2 for a web application.
We though to have disabled error_reporting and display_errors in our production environment.
But if an SQL error occured (It should not in production but ... :-) ), the exception is still displayed:
PDOException
File:
[...]/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:165
Message:
SQLSTATE[42000]: Syntax error or access violation
The query use Doctrine\DBAL\Statement (Doctrine2).
We cannot find where to globally catch this exception.
Ensure you have the correct view_manager config settings.
// config/autoload/global.php
return array(
'view_manager' => array(
'display_not_found_reason' => false,
'display_exceptions' => false,
),
);
Remember that this config is merged; if it's in your main global.php it will take preference over the module.config.php.
How it works
Every time ZF2 encounters and error (an exception) it will always catch the error. Instead of 'rethrowing' the exception information to the screen, the info is added to the MVC event and an 'error' event is triggered (either dispatch.error or render.error depending on where it is within the dispatch loop).
The Zend\Mvc\View\Http\ViewManager attaches 'error listeners' to handle these occurrences - (normally to show the error template). If you're using the standard skeleton application, the default error template will check the display_exceptions option and only render the error if it's enabled.
inside: Zend\Db\Adapter\Driver\Pdo\Connection
search for line :
$this->resource = new \PDO($dsn, $username, $password, $options);
$this->resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
and change it into:
$this->resource = new \PDO($dsn, $username, $password, $options);
$this->resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
I don't know if there is a way to override it globally without changing it in the library itself..
this is how i do it, inside index.php
try {
include ROOT . '/init_autoloader.php';
Zend\Mvc\Application::init(include 'config/application.config.php')->run();
} catch (Exception $e) {
if (IS_DEVELOPMENT_SERVER)
throw $e;
else {
echo('Error : ' . $e->getCode() . " " . $e->getMessage());
}
}

Why is $DebugPreference getting lost in a module?

I have a powershell script and I have set the $DebugPreference to "Continue". However when I call Write-Debug from a module that is called from my script, the $DebugPreference changed to "SilentlyContinue". Why is that? How can I keep $DebugPreference the same as the calling script? Example below
CallingScript.ps1
$DebugPreference = "Continue"
Write-Host "Debug preference: $DebugPreference"
Write-Debug "Checking that debugging works"
Import-Module Logging;
Write-Log "Debug" "Checking that debugging still works!"
Logging.psm1
Function Write-Log
{
param (
[ValidateSet("Error","Warning","Debug","Info")][String]$type,
[String]$logMessage
)
Write-Host "Debug preference: $DebugPreference"
switch($type)
{
"Error" {Write-Error $logMessage;}
"Warning" {Write-Warning $logMessage;}
"Debug" {Write-Debug $logMessage;}
"Info" {Write-Output $logMessage;}
}
}
If I run the script, this is the output:
PS > .\CallingScript.ps1
Debug preference: Continue
DEBUG: Checking that debugging works
Debug preference: SilentlyContinue
PS >
As JPBlanc's link in his comment explains: It is a variable scope issue. The module's scope chain goes directly to the global scope and not through any script scopes. Even if it is imported from a script.
Your code will work if you set $DebugPreference from your script in the global scope, but of course this has influence on more than just your script.
$global:DebugPreference = "Continue"
Another solution in this specific $DebugPreference case is to use the -Debug parameter to pass it along. The downside is that you will have to do this with every command you call.
Write-Log "Debug" "Checking that debugging still works!" -debug:$DebugPreference
A third solution would be to set $DebugPreference at the module level.
$m = Import-Module Logging -PassThru
& $m {$script:DebugPreference = 'Continue'}

Rails 3.1.3 - backtrace_silencers initializer file error

I added the following like to the backtrace_silencers initializer file:
Rails.backtrace_cleaner.add_filter { |line| line.gsub(Rails.root, '') }
I get the following error message in my rails server when an exception occurs:
Error during failsafe response: wrong argument type Pathname (expected
Regexp) .../config/initializers/backtrace_silencers.rb:2:in `gsub'
Which is odd b/c gsub is used as an example here:
http://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.html
Any idea what's going on?
Try
Rails.backtrace_cleaner.add_filter { |line| line.gsub(Rails.root.to_s, '') }