定制 ilham/sql-error-masker 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ilham/sql-error-masker

最新稳定版本:v0.0.1

Composer 安装命令:

composer require ilham/sql-error-masker

包简介

Framework-agnostic SQL error masking and classification.

README 文档

README

A framework-agnostic PHP library to detect, classify, and mask SQL error messages protecting logs and users while preserving useful debug context.

Features

  • Identify SQLSTATE & vendor-specific error codes
  • Classify errors by type (schema/data/query/connection) and severity
  • Mask sensitive data (table names, column values, UUIDs, file paths)
  • Boolean helpers (isDuplicateData, isResourceNotFound, etc.)
  • Multiple log levels (debug, info, warning, error)
  • Works in any PHP project and integrates easily with Laravel or CodeIgniter

Installation

composer require ilham/sql-error-masker

Usage

1. PHP Native Example

<?php

require 'vendor/autoload.php';

use Ilham\SqlErrorMasker\SqlErrorMasker;

$masker = new SqlErrorMasker();

$errorMessage = "SQLSTATE[42S02]: Base table or view not found: 1146 Table `users` doesn't exist";

// Identify the error
$info = $masker->identify($errorMessage);
print_r($info);
/*
Array
(
    [type] => resource_not_found
    [code] => 42S02
    [description] => Resource not found
    [category] => schema
    [severity] => high
)
*/

// Mask for safe logging
echo $masker->mask($errorMessage, SqlErrorMasker::LOG_LEVEL_WARNING);
// Output: SQLSTATE[42S02]: Base table or view not found

// User-friendly message
echo $masker->userMessage($errorMessage);
// Output: The requested resource could not be found.

// Boolean check
if ($masker->isResourceNotFound($errorMessage)) {
    echo "Handle missing table/resource logic here.";
}

2. Laravel Example (via Facade)

Add this Facade in your Laravel app:

<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class SqlErrorMasker extends Facade
{
    protected static function getFacadeAccessor()
    {
        return \Ilham\SqlErrorMasker\SqlErrorMasker::class;
    }
}

Usage in Laravel Controller:

use App\Facades\SqlErrorMasker;

public function store()
{
    try {
        // Some DB operation...
    } catch (\Throwable $e) {
        \Log::error(SqlErrorMasker::mask($e->getMessage(), 'error'));
        return response()->json([
            'message' => SqlErrorMasker::userMessage($e->getMessage())
        ], 500);
    }
}

3. CodeIgniter Example

use Ilham\\SqlErrorMasker\\SqlErrorMasker;

class UserController extends \\CodeIgniter\\Controller
{
    public function store()
    {
        $masker = new SqlErrorMasker();
        try {
            // Some DB operation...
        } catch (\\Throwable $e) {
            log_message('error', $masker->mask($e->getMessage(), SqlErrorMasker::LOG_LEVEL_ERROR));
            return $this->response->setStatusCode(500)
                ->setBody($masker->userMessage($e->getMessage()));
        }
    }
}

Boolean Helper Methods

$masker->isDuplicateData($msg);      // true if duplicate error
$masker->isResourceNotFound($msg);   // true if resource not found
$masker->isQueryError($msg);         // true if syntax/query error
$masker->isConnectionError($msg);    // true if DB connection error

Contributing

Contributions are welcome!

  1. Fork the repo

  2. Create your feature branch:

    git checkout -b feature/my-new-feature
  3. Install dependencies:

    composer install
  4. Run tests:

    composer test
  5. Commit and push your branch

  6. Open a Pull Request

License

MIT License © 2025 Kodikas

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-12