/**************************************************************************************
 *
 *     Project:    kvmstoreserver
 *     File name:  catcher.go
 *     Version:    1.0.0
 *     Date:       2018-01-14
 *
 *     Autor:      Bogdanovic Theodor
 *     Contact:    t.bogdanovic@hotmail.com
 *
 *     License:    GPLv3
 *
 **************************************************************************************/
/**************************************************************************************
 * Copyright 2018 Bogdanovic Theodor
 *
 * This file is part of kvmstoreserver.
 *
 * kvmstoreserver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * kvmstoreserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kvmstoreserver.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************************/
/**************************************************************************************
 *  Further explanations:  This source code is under the license specified by the
 *                         Autor above. Core components or other packages are still
 *                         in source code form, under the license specified by the
 *                         respective author.
 **************************************************************************************/

package errorHandling

import (
	"encoding/json"
	es "essentials"
	"net/http"
)

// IfErrBuildJSONAndOrPanic accepts the responewriter, the origianl error,
// an HTTP congestion code and its own message. If the error !s= nil, you can
// use the control enum to define whether this function should trigger a panic
// or simply output an error per result
func IfErrBuildJSONAndOrPanic(writer http.ResponseWriter,
	origErr error, statusCode int, customErrMsg string, doPanic es.Control) Result {

	if origErr != nil {

		if customErrMsg == "" {
			customErrMsg = "No further description"
		}

		innerAswerStruct := &struct {
			StatusCode           int    `json:"status_code"`
			Description          string `json:"description"`
			OriginalErrorMessage string `json:"original_error_message"`
		}{
			StatusCode:           statusCode,
			Description:          customErrMsg,
			OriginalErrorMessage: origErr.Error(),
		}

		jsonOutput, err := json.Marshal(innerAswerStruct)
		if err != nil {
			panic("Can not recover panic, kill this routine without to give the client a answer")
		}

		writer.WriteHeader(statusCode)
		writer.Write(jsonOutput)

		if doPanic == es.DoPanic {
			panic(innerAswerStruct.OriginalErrorMessage)
		}

		return Nok
	}

	return Ok
}
