Can mgo return error different than QueryError or ErrNotFound? What with database connection errors?
Is it a good practise to panic on error different than ErrNotFound and recover on the top of http handlers stack with something like pretty response with status 500?
The set of errors returned by mgo is not constrained, because it does a number of operations underneath that may also return errors (DNS resolution, connection establishment, timeouts, etc). So the proper way to handle errors with mgo is the same as most places: handle the ones you do know about and have custom logic for, and bail out on the ones you don't. Good bailing out encompasses undoing any local side-effects (close/remove locally created files, etc), and then returning the error to the caller, perhaps decorated or wrapped with custom context information.
I wouldn't panic on such errors. Panics are usually for abnormal situations, when the developer did something wrong with the API, or the environment is seriously damaged and the best course of action is to stop altogether, for example. A connection with the database (or anything network related) should be expected to fall down every once in a while, and handled appropriately rather than just logging an undistinguishable crash.
If you have more details and would like to talk further, please come over to the mailing list.
I believe you can check any error with LastError. Most of the error returning functions return a standard Go error that should be checked upon function return.
Usually, in Go, you'd want some very special use case before resorting to panic / recover. It's best practice to handle the errors as they arise.
For more info see Error handling and Go and Defer, Panic, and Recover from The Go Blog.
Related
In Ethereum Events are clearly defined, each one is a data structure used to signal some action. In Near, env::log are messages.
Example:
In Ethereum we use logs for most of the token state changes
NEP-21 dosn't say anything about logs.
Is there any point of using logs in near, except "debug" / return user information? Should logs be standarized ?
Maybe it's better to have this discussion in some other place...?
Following on that:
Transaction arguments are serialized and clearly visible. Is there any point to log transaction arguments?
For example: in Ethereum token transfer functions, all arguments are recorded additionally in an event. There are few reasons for that:
With events we should be able to recreate a state of the contract;
it's more readable while browsing a blockchain.
But in case of transfer, I don't think there is any added value, because we don't log anything else than the function arguments.
We haven't added analog of Ethereum events into NEAR yet. Contracts that want to signal about some event need to return it as part of the method result like here. Therefore our env::log is currently for informational purposes only and can be used for debugging and such.
In general, our contracts can choose how to serialize arguments, so it might be useful for the contract to log its own arguments since it might be using a complex serialization mechanism for them.
It might be useful to have logs for complex contracts with complex cross contract calls and callbacks so that it can inform the user how it arrived to a specific result.
I am using spring-integration, and I have messages that goes through an int:chain with multiple elements: int:service-activator, int:transformers, etc. In the end, a message is sent to another app's Rest endpoint. There is also an errorHandler that will save any Exception in a text file.
For administration purpose, I would like to keep some information about what happened in the chain (ex: "this DB call returned this", "during this transformation, this rule was applied", etc.). This would be equivalent to a log file, but bound to a Message. Of course there is already a logger, but in the end, I need to create (either after the Rest called is made, or when an error occurs) a file for this specific Message with the data.
I was wondering if there was some kind of "context" for the Message that I could call through any part of the chain, and where I could store stuff. I didn't found anything in the official documentation, but I'm not really sure about what to look for.
I've been thinking about putting it all in the Message itself, but:
It's an immutable object, so I would need to rebuild it each time I want to add something to its header (or the payload).
I wouldn't be able to retrieve any new data from the error handler in case of Exception, because it takes the original message.
I can't really add it to the payload object because some native transformers/service-activators are directly using it (and that would also mean rewriting a lot of code ...)
I've been also thinking to some king of "thread-bound" bean that would act as a context for each Message, but I see too many problem arising from this.
Maybe I'm wrong about some of these ideas. Anyway, I just need a way to keep data though multiple element of a Spring integration chain and also be able to access it in the error handler.
Add a header, e.g. a map or list, and add to it in each stage.
The framework does something similar when message history is enabled.
Using Event-machine and Ruby. Currently I'm making a game were at the end of the turn it checks if other user there. When sending data to the user using ws.send() how can I check if the user actually got the data or is alternative solution?
As the library doesn't provide you with access to the underlying protocol elements, you need to add elements to your application protocol to do this. A typical approach is to add an identifier to each message and response to messages with acknowledgement messages that contain those identifiers.
Note that such an approach will only help you to have a better idea of what has been received by a client. There is no assurance of particular state in the case of errors. An example would be losing a connection after the client as sent an ACK, but the service has not received it.
As a result of the complexity I just mentioned, it is often easier to try to make most operations idempotent - that is able to be replayed without detriment to the system, and to replay readily during/after error conditions. You may additionally find a way to periodically synchronize the relevant state entirely, to avoid the long term continuation of minor errors introduced by loss of data/a connection.
Is there any downside to using a promise with only .catch section, but without .then at all?
I'm asking about the cases where the resolution result is not needed, only error handling.
Is this a good pattern to rely on .catch only and skip .then?
Or is it something that depends on which promise implementation it is?
Conceptually, there's nothing wrong with an operation that only has an error handler and has nothing else to do upon successful completion. If that's all it needs, then that's fine. For example, suppose you're updating a server with some new data from the client. If the data is successfully sent to the server, there's nothing else to do because the operation is complete, but if there's an error, then there is perhaps something else to do (retry, inform the user, correct the data based on the error code, etc...).
To comment on whether that's the right way to design your specific code, we'd have to see the actual code and understand what it is doing and then form an opinion on whether that is the best way to structure that specific code.
If I was designing a general purpose function, I'd certainly provide both completion (resolving the promise) and error (rejecting the promise) so the caller could hook into either one. But it is really up to the caller which events they want to know about and if only the error matters, then just having a .catch() is fine.
Assuming that GetDC(hWnd) is called as the first instruction on WM_CREATE, for window hWnd, is it possible for GetDC() to return NULL?
I am concerned about the possibility of GetDC() failing due to lack of resources. When that happens should I check for errors? And what should I do if the call fails? Do the Win32 API function raise exceptions or should I raise exceptions?
GetDC() can indeed fail, no matter when or where you call it. You ought to take a rather pessimistic view when dealing with the API and be prepared for any API function to fail. As you suggest, one possible reason for failure is exhaustion of system resources, e.g. kernel handles, GDI objects etc.
So you should always check for errors. And not just GetDC(), every single call to an API function should have its return value checked.
The Windows API does not signal errors by raising exceptions. Instead the errors are always signalled through the values returned by the API function. You need to consult the documentation to understand how each individual function reports errors.
If you encounter a failure in a GDI function like GetDC() then there's not a lot you can do. In the scenario you describe I would log the error or report it to the user, and then terminate execution. There's generally no recovery from a failure of GetDC().
The pain of checking for errors every time an API function is called is one of the reasons why we have so many frameworks that wrap the low level API. A good framework will do the error checking for you and convert any errors into exceptions. Using a good framework allows you to concentrate on the normal flow of execution and not littering your code with handling code for exceptional cases.