Herokuアップデートをかましたらnode-postgres(pg)のバージョンが上がってExpressで作ってたWEBサイトが動かなくなった話。

システム開発Tips
heroku

発生したエラーの現象と原因

2020年6月頃に、HerokuにExpressでリリースしたWEBアプリケーションが動かなくなりました。

Herokuから更新の案内が来てポチッとボタンを教えてアプリケーションをアップデートしたら動かなくなったようで、エラー情報は以下のように出ていました。

const pool = new require('pg').Pool(config.db.postgres);
                               ^
// StackTraceは以下
/*
TypeError: Class constructor BoundPool cannot be invoked without 'new' at Object.<anonymous>
*/

エラーの箇所から見て間違いなくpgのバージョンアップのせいですね。
そして、これはもともとバグってみたいで、require('pg')をnewしてますね。。。

いままでなんとなしに動いてたのが、newなしでは動かんで!といってくれてるので良しとしましょうか。

発生したエラーの改善方法

コードの修正方法(newを追加)

const pg = require('pg');
const pool = new pg.Pool(config.db.postgres);

上記のようにコードを修正したら動いた!のですが、次はDB接続時に証明書エラーが出てくるようになった。
自己証明書とかいままでいらなかったのになんで・・・。

エラーは以下のように出力されます。

self signed certificate Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1474:34)
    at TLSSocket.emit (events.js:310:20)
    at TLSSocket._finishInit (_tls_wrap.js:917:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:687:12)

self signed certificate?なんでだなんでだー!
リファレンスを漁っていたら、SSLの指定方法が変わったよと以下の記述がありました。

Now we will use the default ssl options to tls.connect which includes rejectUnauthorized being enabled. This means your connection attempt may fail if you are using a self-signed cert. To use the old behavior you should do this:

コードの修正方法(SSLオプションを変更)

// const client = new Client({ ssl: true })
const client = new Client({ ssl: { rejectUnauthorized: false } }) // こちらになりました。

なるほど。とりあえず、オプション指定を変更して事なきを得ました。

newの件について公式情報

そして、最初のnewの件ですが、それもリファレンスに記載がありました。

make pg.Pool an es6 class
This makes extending pg.Pool possible. Previously it was not a "proper" es6 class and class MyPool extends pg.Pool wouldn't work.

pg@8.0からes6に準拠したよとのこと。なるほどなぁ。。。

フォローお願いします!

コメント

タイトルとURLをコピーしました