microo8 / plgo

easily create postgresql extensions in golang; moved to gitlab.com/microo8/plgo
292 stars 23 forks source link

How to catch and display an exception in the process of work extension ? #26

Closed AoAnima closed 5 years ago

AoAnima commented 5 years ago

Hello! There are situations when expansion stops working with panic, psql: server unexpectedly closed connection Most likely the server has stopped working due to a failure before or during the request. The connection to the server was lost. Attempting reset: Failed. How to make detailed error information displayed as if it were a regular program? or jdbc_driver: java.io.EOFException

Привет! Бывают ситуации когда расширение прекращает работу с паникой, psql : сервер неожиданно закрыл соединение Скорее всего сервер прекратил работу из-за сбоя до или в процессе выполнения запроса. The connection to the server was lost. Attempting reset: Failed. как сделать чтобы выводилась подробная информация об ошибке как если бы это была обычная программа? jdbc_driver пишет : java.io.EOFException

microo8 commented 5 years ago

panic typically stops the process running the code in the extension. This also happens when you make a C extension that runs into a fault (like segmentation fault, or stack overflow, ...). This is hard to catch, but when it is a Go panic, you can always use recover.

panic_test.go:

package main

func FunctionThatPanics() {
        logger := plgo.NewErrorLogger("", log.Ltime|log.Lshortfile)
        defer func() {
                if r := recover(); r != nil {
                        logger.Println("Recovered in FunctionThatPanics", r)
                }
        }()
        var x []int
        x[0] = 1
}
$ cd <path_to_panic_test>
$ plgo
$ cd build
$ sudo make install
$ psql -U root postgres
psql (11.1)
Type "help" for help.
postgres=# CREATE EXTENSION panic_test;
CREATE EXTENSION
postgres=# select FunctionThatPanics();
ERROR:  09:02:39 package.go:21: Recovered in FunctionThatPanics runtime error: index out of range
AoAnima commented 5 years ago

Perfectly! Thank you!