如何设计友好的错误提示,避免暴露服务端内部堆栈信息?

解读

国内面试官问这道题,核心想验证三点:

  1. 安全意识——是否知道堆栈、SQL 语句、路径等敏感信息一旦返回到客户端,会被恶意利用;
  2. 工程化能力——能否用 VB 现有机制把“技术异常”与“用户提示”彻底隔离;
  3. 用户体验观——能否让普通用户一眼看懂,同时给运维留下可追踪的线索。
    回答时先给原则,再给代码级落地,最后补一句“上线后怎么审计”,基本就能拿到高分。

知识点

  1. Try…Catch…Finally 结构化异常处理(VB.NET)/ On Error 语句(VB6)。
  2. Exception 对象层次:SqlException、IOException、ArgumentException 等,需分类处理。
  3. 自定义异常类(Inherits Exception)与错误码枚举,方便多语言提示。
  4. 日志框架:NLog、log4net、EventLog,记录 TraceId、时间、用户名、参数哈希。
  5. 统一错误返回模型:
    Public Class ApiResult(Of T)
    Public Property Code As Integer
    Public Property Msg As String
    Public Property Data As T
    Public Property TraceId As String
    End Class
  6. Web.config 的 customErrors 或 ASP.NET Core 的 UseExceptionHandler,禁止 remoteAccess="true"。
  7. VB6 旧系统可用“标准模块+Err.Raise+中央错误窗体”模拟隔离层。
  8. 国家等保 2.0 要求:异常信息不得包含系统路径、数据库字段、中间件版本号。

答案

以 VB.NET WinForms+WebAPI 混合项目为例,分四层实现:

  1. 全局异常入口
    ‘Program.vb
    AddHandler AppDomain.CurrentDomain.UnhandledException,
    Sub(sender, e)
    Dim ex = DirectCast(e.ExceptionObject, Exception)
    Dim traceId = Guid.NewGuid().ToString("N")
    Logger.Fatal(ex, "Unhandled-{TraceId}", traceId)
    MessageBox.Show("系统开小差了,错误号:" & traceId & ",请联系客服!",
    "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    Environment.Exit(1)
    End Sub

  2. 业务层只抛自定义异常
    Public Class UserService
    Public Function GetUser(id As Integer) As UserDto
    If id <= 0 Then Throw New BizException(1001, "用户编号必须大于零")
    Try
    Return _repo.Find(id)
    Catch ex As SqlException
    Throw New BizException(2001, "数据库查询失败", ex) ‘内部 ex 仅用于日志
    End Try
    End Function
    End Class

  3. WebAPI 统一过滤器
    Public Class GlobalExceptionFilter : Inherits ExceptionFilterAttribute
    Public Overrides Sub OnException(actionContext As HttpActionExecutedContext)
    Dim traceId = Activity.Current?.Id ?? Guid.NewGuid().ToString("N")
    Dim code = 500
    Dim msg = "服务器繁忙"
    Select Case actionContext.Exception
    Case IsNot Nothing
    If TypeOf actionContext.Exception Is BizException Then
    Dim be = CType(actionContext.Exception, BizException)
    code = be.Code
    msg = be.Message
    End If
    Logger.Error(actionContext.Exception, "API-{TraceId}", traceId)
    End Select
    actionContext.Response = actionContext.Request.CreateResponse(
    HttpStatusCode.OK,
    New With {.Code = code, .Msg = msg, .TraceId = traceId})
    End Sub
    End Class

  4. 前端友好提示
    WinForms:用 Toast 控件弹出返回的 Msg,若 Code<>0 自动把 TraceId 复制到剪贴板,方便用户贴给客服。
    Web:Vue/Element 直接 this.$message.error(res.Msg + ' 错误号:' + res.TraceId)。

上线前检查清单:

  • web.config 中 <customErrors mode="On" defaultRedirect="Error.html"/>
  • 发布脚本删除 *.pdb 文件;
  • 日志文件写入服务器本地,不共享到虚拟目录;
  • 渗透测试验证:用 Fiddler 触发 500,响应体中不得出现 “at System.Data.SqlClient” 等字样。

拓展思考

  1. 微服务链路:可把 TraceId 写入 HTTP Response Header “X-Trace-Id”,网关层统一收集,再对接 SkyWalking 或 Jaeger,实现跨服务追踪。
  2. 多语言场景:将 Msg 做成资源字典,错误码不变,根据 Accept-Language 动态切换,避免硬编码中文。
  3. 前端埋点:在 catch 里把 TraceId 随行为日志一起回传,用户投诉时直接按 TraceId 串联前后端日志,10 分钟定位问题。
  4. 灰度降级:对特定 BizException 可返回缓存数据或默认值,而不是直接弹“系统错误”,提高可用性。
  5. 合规演进:若系统需过等保三级,建议把错误日志脱敏后再入 Elasticsearch,敏感字段如身份证号、手机号全量打码,防止运维人员越权查看。