4267a229创建于 2024年8月2日历史提交
/*
    Copyright (c) [2023] [squallzhao]
    fountain is licensed under APACHE LICENSE, VERSION 2.0.
    You can use this software according to the terms and conditions of the APACHE LICENSE, VERSION 2.0.
    You may obtain a copy of APACHE LICENSE, VERSION 2.0 at: https://www.apache.org/licenses/LICENSE-2.0
*/
package microservice.web.handler

import std.collection.*
import microservice.exception.*
import microservice.web.server.reqres.*

public class GlobalExceptionHandler{
    static var  map = HashMap<String, IExceptionHandler>()
    static var defaultExceptionHandler:IExceptionHandler = DefaultExceptionHandler();

    public static func add(msg: String, handler:IExceptionHandler) : Unit{
         map.put(msg,handler)
    }
    
    public static func setDefaultExceptionHandler(handler:IExceptionHandler){
         defaultExceptionHandler = handler
    }

    public static func handleException(req: HttpServletRequest, res: HttpServletResponse, e: Exception): Unit{
       var h:Option<IExceptionHandler> = map.get(e.message);
       if (let Some(v) <- h){
          v.handle(req, res, e)
       } else{
          defaultExceptionHandler.handle(req,res,e)
       }
    }
}