does anyone know the syntax for the try-catch of the following realm function is?
realm.write() {
realm.add(whatever)
}
I'm getting the following error:
call can throw but it is not marked with 'try' and the error is not
handled
From what I imagine realm.write() can throw an exception. In Swift 2 you handle exceptions with do/catch and try.
I suspect that you should do something like this:
do {
try realm.write() {
realm.add(whatever)
}
} catch {
print("Something went wrong!")
}
If realm.write() throws an exception, print statement will be invoked immediately.
It looks like an NSError gets thrown. See the Swift 2.0 source
Adding on to #tgebarowski's answer:
do {
try self.realm.write {
realm.add(whatever)
}
} catch let error as NSError {
print("Something went wrong!")
// use the error object such as error.localizedDescription
}
You can also try
try! realm.write {
realm.add(whatever)
}
Related
in Strapi 4.0, i want to validate the input before saving. So i created lifecycles.js file as per the documentation and added the code:
module.exports = {
beforeCreate(event) {
//validation login here;
if (!valid) {
throw strapi.errors.badRequest('Invalid Entry');
}
},
}
How ever throw strapi.errors.badRequest('Invalid Entry'); is giving an error :
Cannot read property 'badRequest' of undefined
My guess is the Strapi v4 changed it from version 3. I looked everywhere but couldn't find a solution.
Any idea on how to handle error in lifecycles.js?
I had a similar situation with a forbidden error. I got to do it importing a class from #strapi/utils/lib/errors.js
const { ForbiddenError } = require("#strapi/utils").errors;
...
if (!authorized) {
throw new ForbiddenError(errorMessage);
}
You can show the list of errors based on your requirement
const { ValidationError } = require("#strapi/utils").errors;
...
if (formValidationError) {
throw new ForbiddenError("Fill the form");
}
Strapi comes with a lot of error response functions here are they
HttpError,
ApplicationError,
ValidationError,
YupValidationError,
PaginationError,
NotFoundError,
ForbiddenError,
PayloadTooLargeError,
UnauthorizedError,
PolicyError,
I have an Apollo GraphQL server, where I want to only report internal server errors (not errors extending ApolloError like AuthenticationError, UserInputError, etc.).
Here is the plugin that I wrote that catches internal server errors and reports them:
const errorReportingPlugin = {
requestDidStart(_) {
return {
didEncounterErrors(ctx) {
// If we couldn't parse the operation, don't do anything
if (!ctx.operation) return
for (const err of ctx.errors) {
// Don't report errors extending ApolloError like AuthenticationError, UserInputError, etc.
if (err instanceof ApolloError) {
continue
}
// report error here...
}
}
}
}
}
However err instanceof ApolloError returns false when I throw AuthenticationError, which extends ApolloError.
So I tried to check the class of the err by printing the constructor name and I got GraphQLError.
console.log(err.constructor.name)
Does anyone know how to avoid reporting all errors extending ApolloError?
The solution is to check whether err.originalError (not err) is an instance of ApolloError like this:
if (err.originalError instanceof ApolloError) {
// don't report error since it is a user facing error like AuthenticationError, UserInputError, etc.
}
credit to #xadm
I would like to change the way queue exceptions of type "A queued job has been attempted too many times. The job may have previously timed out." are logged.
I tried adding the following to the \Exception\Handler::report() method but I still see these showing up as ERROR in my logs.
public function report(Exception $exception)
{
if ($exception instanceof \Illuminate\Queue\MaxAttemptsExceededException) {
try {
$logger = $this->container->make(LoggerInterface::class);
} catch (Exception $ex) {
throw $exception; // throw the original exception
}
$logger->warning($e);
return;
}
parent::report($exception);
}
I also get alerts to a Slack channel (the most annoying part) for these when the min level is ERROR. I want to transform it to a WARNING so I prevent that from happening.
$slackHandler = new SlackHandler(
config('slack.api_token'),
config('slack.channel'),
'Laravel Error',
true,
null,
Logger::ERROR,
true,
false,
true
);
Admittedly this is similar to another question I asked, but I appear to have implemented it wrong.
Let's say I have the following task modifying my Alloy compile process (alloy.jmk build configuration file)
task("pre:compile", function (e, log) {
// execute something that may throw an error
try {
// ...
}
catch(err) {
// do some custom error handling
// ...
// !!! need to stop the build here !!!
}
log.info("My task completed!");
});
How can I stop the build in the catch-clause? Of course I could just remove the try-catch but then my custom error handling won't be executed...
Well, answering my own question here... It seems it was too easy ... Simply throw a custom error inside the catch-statement like so
task("pre:compile", function (e, log) {
// execute something that may throw an error
try {
// ...
}
catch(err) {
// do some custom error handling
// ...
throw('throwing some error to stop the build');
}
log.info("My task completed!");
});
I'm using Parse plugin 1.5.5 for Unity 5.1.3 in order to communicate with Parse.com in Unity. I'm getting FormatException when trying to connect to Parse when there is no internet connection with following code. I was wondering if this is a bug of Parse plugin or anything I did wrong.
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Data");
query.FirstAsync().ContinueWith (task =>
{
if (task.IsCanceled)
{
Debug.Log ("parse canceled");
}
else if(task.IsFaulted)
{
Debug.Log ("parse error");
}
else
{
Debug.Log ("parse retrieved");
}
});
Error code is following.
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono- runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629)
Parse.PlatformHooks+HttpRequestUnity.getStatusCode (UnityEngine.WWW www)
Parse.PlatformHooks+HttpRequestUnity+<>c__DisplayClass40+<>c__DisplayClass46.<ExecuteAsync>b__3a (UnityEngine.WWW www)
Parse.PlatformHooks+<>c__DisplayClass24.<RegisterNetworkRequest>b__23 ()
Parse.PlatformHooks+<RunDispatcher>d__2e.MoveNext ()
UnityEngine.Debug:LogException(Exception)
Parse.<RunDispatcher>d__2e:MoveNext()
Thank you,