Returning status code in laravel - http

I am a total noob at Laravel
public function stageFiles($user, $project) {
try
{
$path = Input.get("item");
}
catch (Exception $e)
{
$exceptionMessage = $e->getMessage();
$responseText = json_encode($exceptionMessage);
}
}
What I want to do is return the response text and a status code, 500, how can I do this?

This is how you send a 500:
public function stageFiles($user, $project) {
try
{
$path = Input.get("item");
}
catch (Exception $e)
{
$exceptionMessage = $e->getMessage();
$responseText = json_encode($exceptionMessage);
return Response::make($responseText, 500);
}
}

Related

How can I fail wp_action_scheduler cron job without site break?

Wp action-scheduler job is running successfully but I want it to fail when any exception comes in the code.
try {
$records = some records in array;
foreach ($records as $r) {
$sku = $r["SKU"] ?? null;
if (!isset($sku)) {
throw new \Exception("SKU is missing");
return;
}
$product_id = Wp::wc_get_product_id_by_sku($sku);
Wp::update_post_meta($product_id, "_regular_price", 500);
}
} catch (\Exception $e) {
echo($e);
**//Here I want to fail the action scheduler job**
return false;
}
In my case, everything works correctly but it should be failed schedule job if comes into catch (\Exception $e){---} without site break.

How to check if the record exist in xamarin

public bool insertIntoTablePerson(Person person)
{
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db")))
{
connection.Insert(person);
return true;
}
} catch (SQLiteException ex) {
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
generally, if the item already has a non-zero PK then it already exists
if (item.ID != 0)
{
return database.UpdateAsync(item);
}
else {
return database.InsertAsync(item);
}

How to give keycodes from a properties file in javafx

Currently i have configured JavaFX keyEvents for key board controllers for each task. What i want is to configure there keys from a properties file rather than hard coding in the code.
The implementation :
final KeyCombination keyCombinationShiftC = new KeyCodeCombination(
KeyCode.ENTER, KeyCombination.CONTROL_DOWN);
javafx.event.EventHandler<javafx.scene.input.KeyEvent> handler = event -> {
if (keyCombinationShiftC.match(event)) {
try {
if (finalSubTotalPrice > 0) {
paymentAction();
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle(app.values.getProperty("WARNING_TITLE"));
alert.setHeaderText(app.values.getProperty("INVALID_NO_OF_ITEMS"));
alert.setContentText(app.values.getProperty("INVALID_NO_OF_ITEMS_DIALOG"));
alert.showAndWait();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
switch (event.getCode()) {
case F10:
try {
removeAction();
} catch (Exception e1) {
e1.printStackTrace();
}
break;
case F1:
try {
searchField.requestFocus();
} catch (Exception e1) {
e1.printStackTrace();
}
break;
case F5:
try {
customVatDiscountCalculation();
} catch (Exception e1) {
e1.printStackTrace();
}
break;
}
The ENTER+CNTRL_DOWN, F5, F10, F1 keys need to be assign for other keys without changing the code using a properties file.
If i try to get the Strings from the properties file, i fail to do that. as below. It says a constant expression is required for the case.
public static final KeyCode REMOVE_KEY = KeyCode.getKeyCode(app.values.getProperty("REMOVE_KEY"));
switch (event.getCode()) {
case REMOVE_KEY:
try {
removeAction();
} catch (Exception e1) {
e1.printStackTrace();
}
break;
}

Delete Records from a database

I am trying to delete set of records under my ASP.NET Application - API Controller. Here is my code from API Controller:
public JsonResult Delete([FromBody]ICollection<ShoppingItemViewModel> vm)
{
if (ModelState.IsValid)
{
try
{
var items = Mapper.Map<IEnumerable<ShoppingItem>>(vm);
_repository.DeleteValues(items, User.Identity.Name);
return Json(null);
}
catch (Exception Ex)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(null);
}
}
else
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(null);
}
}
And here is my AngularJS Controller part taking care of this:
$scope.RemoveItems = function () {
$scope.isBusy = true;
$http.delete("/api/items", $scope.items)
.then(function (response) {
if (response.statusText == "OK" && response.status == 200) {
//passed
for (var i = $scope.items.length - 1; i > -1; i--) {
if ($scope.items[i].toRemove == true) {
$scope.items.splice(i, 1);
}
}
}
}, function (err) {
$scope.errorMessage = "Error occured: " + err;
}).finally(function () {
$scope.isBusy = false;
});
}
For unknown reason, this works like a charm in my POST method but not in the Delete Method. I believe that the problem might be caused by the fact, that DELETE method only accepts Integer ID?
If that is the case, what is the correct way how to delete multiple items with one call?

Google_Service_Exception error 500 not caught

I am using the google api client to get a list of blogger posts (see below code).
try {
// create service and get data
$blogger = new \Google_Service_Blogger($this->client);
// https://developers.google.com/blogger/docs/3.0/reference/posts/list
// GET https://www.googleapis.com/blogger/v3/blogs/blogId/posts
if ($this->cache->contains('posts')) {
$posts = $this->cache->fetch('posts');
}
else {
$posts = $blogger->posts->listPosts($this->bloggerId, $optParams);
$this->cache->save('posts', $posts, 600); // 600 = 10 mins
}
return $posts;
} catch (apiServiceException $e) {
// Error from the API.
//print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
} catch (Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
This works fine on most occasions, but occasionally I get an API error which is not caught.
Google_Service_Exception: Error calling GET https://www.googleapis.com/blogger/v3/blogs/8894809254783984374/posts?maxResults=5: (500) Backend Error at /var/www/releases/20140607051057/vendor/google/apiclient/src/Google/Http/REST.php:79)"} []
Does anyone know why this error is thrown, and how I can deal with it?
Cheers
The problem is with the namespaces. You can use the generic:
} catch (\Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
or, more specific:
try {
...
} catch (\Google_Service_Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
Exception is the base class for all Exceptions. See http://php.net/manual/en/class.exception.php

Resources