Skip to content

ユーザー登録時グループを選択せず保存した場合のエラーメッセージに「 データベース処理中にエラーが発生しました。」と表示される件を修正#4478

Open
otsuka-star wants to merge 1 commit into
baserproject:5.3.xfrom
otsuka-star:5.3.x-15

Conversation

@otsuka-star

@otsuka-star otsuka-star commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

@ryuring

こちらのレビューをお願い致します。

改修内容:
ユーザー登録時にグループを選択せず保存した場合のエラーメッセージを「 データベース処理中にエラーが発生しました。ユーザーグループを指定してください。」→「ユーザーグループを指定してください。」へ修正

要因:
ユーザー登録時(UsersController::add())のcatchがPersistenceFailedExceptionThrowableの2種類しか分岐しておらず、BcExceptionがThrowableの派生型のようなので後者のThrowableのcatchが適用され「 データベース処理中にエラーが発生しました。」というエラーメッセージが表示されているようでした。

改修方法:
catch (BcException $e)をPersistenceFailedExceptionと\Throwableの間に追加し、「ユーザーグループを指定してください。」というエラーメッセージのみ表示されるようにいたしました。

} catch (\Cake\ORM\Exception\PersistenceFailedException $e) {
$user = $e->getEntity();
$this->BcMessage->setError(__d('baser_core', '入力エラーです。内容を修正してください。'));
} catch (BcException $e) {

@teratai3 teratai3 Jul 23, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BcException とThrowableの例外を表記修正のためだけに使い分けている例がコアだとかなり少ない気がするのですが...一応コア側の実装も確認されましたか?
表記を調整するためにそこまでやるのか、少し疑問に思いました。

管理画面とWEBAPI側

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryuring

こちら調べてみると、ユーザーグループ削除の際①-1: Api/Admin/UserGroupsController.php:149-153行目と①-2: Admin/UserGroupsController.php:149-153行目、ブログコメントの登録時②-1: plugins/bc-blog/src/Controller/BlogController.php:339-348行目と②-2: plugins/bc-blog/src/Controller/Api/Admin/BlogCommentsController.php:193-199行目などで、BcExceptionとThrowableの例外を表記修正のために使い分けが行われていますが、このパターンを取り入れる形で宜しいでしょうか?

①-1:
        } catch (BcException $e) {
            $this->BcMessage->setError($e->getMessage());
        } catch (\Throwable $e) {
            $this->BcMessage->setError(__d('baser_core', 'データベース処理中にエラーが発生しました。'));
        }
①-2:
        } catch (BcException $e) {
            $this->setResponse($this->response->withStatus(400));
            $message = $e->getMessage();
        } catch (\Throwable $e) {
            $message = __d('baser_core', 'データベース処理中にエラーが発生しました。') . $e->getMessage();
            $this->setResponse($this->response->withStatus(500));
        }
②-1:
        } catch (BcException $e) {
            $message = $e->getMessage();
            return $this->response->withStatus(400)->withStringBody(json_encode([
                'message' => $message
            ]));
        } catch (Throwable $e) {
            $message = __d('baser_core', 'データベース処理中にエラーが発生しました。') . $e->getMessage();
            return $this->response->withStatus(500)->withStringBody(json_encode([
                'message' => $message
            ]));
②-2:
        } catch (BcException $e) {
            $message = $e->getMessage();
            $this->setResponse($this->response->withStatus(400));
        } catch (Throwable $e) {
            $message = __d('baser_core', 'データベース処理中にエラーが発生しました。') . $e->getMessage();
            $this->setResponse($this->response->withStatus(500));
        }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@otsuka-star こちらの件、ユーザーグループのバリデーションの問題ですので、UsersService 側で、BcExceptionを投げているのが間違いですね。PersistenceFailedException を投げる仕様に調整すべきです。

} catch (\Cake\ORM\Exception\PersistenceFailedException $e) {
$user = $e->getEntity();
$this->BcMessage->setError(__d('baser_core', '入力エラーです。内容を修正してください。'));
} catch (BcException $e) {

@teratai3 teratai3 Jul 23, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらを追加するなら...
ユーザーグループコントローラーと同じようにWEB API側も追加するべきでは無いでしょうか?

新規追加と更新

なお、別PRで対応いただく形で認識していれば、問題ありませんが念の為記載します。

@ryuring

ryuring commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

@otsuka-star 回答いれています

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants