...

Source file src/scheduler/errors/reply_errors.go

Documentation: scheduler/errors

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019 - 2022. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  package errors
    20  
    21  import (
    22  	"encoding/json"
    23  	"net/http"
    24  	"scheduler/utils"
    25  )
    26  
    27  type ErrorReply struct {
    28  	Code    int    `json:"code,omitempty"`
    29  	Message string `json:"message,omitempty"`
    30  	Err     error  `json:"err,omitempty"`
    31  }
    32  
    33  const (
    34  	GenericError                int = 1
    35  	DBError                     int = 2
    36  	GenericNotFoundError        int = 3
    37  	InputNotValid               int = 4
    38  	FaasConnectError            int = 5
    39  	MarshalError                int = 6
    40  	ServiceNotValid             int = 100
    41  	GenericDeployError          int = 200
    42  	GenericOpenFaasError        int = 300
    43  	JobCannotBeScheduledError   int = 400
    44  	JobDeliberatelyRejected     int = 401
    45  	CannotRetrieveAction        int = 402
    46  	JobCouldNotBeForwarded      int = 403
    47  	PeerResponseNil             int = 404
    48  	CannotRetrieveRecipientNode int = 405
    49  
    50  	DBDuplicateKey int = 11000
    51  )
    52  
    53  var errorMessages = map[int]string{
    54  	1: "Generic Error",
    55  	2: "DB Error",
    56  	3: "Not Found",
    57  	4: "Passed input is not correct or malformed",
    58  	5: "Could not contact OpenFaaS backend",
    59  	6: "Cannot marshal the struct",
    60  	// service validation
    61  	100: "Passed service is not valid",
    62  	// deploy
    63  	200: "Error while deploying the service",
    64  	// openfaas
    65  	300: "OpenFaas generic error, see logs",
    66  	// scheduler
    67  	400: "Job cannot be scheduled due to physical limitation (e.g. queue full)",
    68  	401: "Job have been deliberately rejected by the scheduler",
    69  	402: "It is not possible to retrieve the scheduling action",
    70  	403: "It was not possible to forward the request to the neighbor",
    71  	404: "Peer replied with nil response",
    72  	405: "Recipient node to which the job must be forwarded cannot be retrieved",
    73  	// mongo
    74  	11000: "A key is duplicated",
    75  }
    76  
    77  var errorStatus = map[int]int{
    78  	1: 500,
    79  	2: 500,
    80  	3: 404,
    81  	4: 400,
    82  	5: 500,
    83  	6: 500,
    84  	// service validation
    85  	100: 400,
    86  	// deploy
    87  	200: 500,
    88  	// openfaas
    89  	300: 500,
    90  	// scheduler
    91  	400: 500,
    92  	401: 503,
    93  	402: 500,
    94  	403: 500,
    95  	404: 500,
    96  	405: 500,
    97  	// mongo
    98  	11000: 400,
    99  }
   100  
   101  func GetErrorJson(errorCode int) (int, string, error) {
   102  	var errorReply = ErrorReply{Code: errorCode, Message: errorMessages[errorCode]}
   103  	errorReplyJSON, err := json.Marshal(errorReply)
   104  
   105  	if err != nil {
   106  		return -1, "", err
   107  	}
   108  
   109  	return errorStatus[errorCode], string(errorReplyJSON), nil
   110  }
   111  
   112  func GetErrorJsonMessage(errorCode int, msg string) (int, string, error) {
   113  	var errorReply = ErrorReply{Code: errorCode, Message: msg}
   114  	errorReplyJSON, err := json.Marshal(errorReply)
   115  
   116  	if err != nil {
   117  		return -1, "", err
   118  	}
   119  
   120  	return errorStatus[errorCode], string(errorReplyJSON), nil
   121  }
   122  
   123  func ReplyWithError(w *http.ResponseWriter, errorCode int, customHeaders *map[string]string) {
   124  	errorStatusCode, errorResponseJson, _ := GetErrorJson(errorCode)
   125  
   126  	utils.HttpSendJSONResponse(w, errorStatusCode, errorResponseJson, customHeaders)
   127  }
   128  
   129  func ReplyWithErrorMessage(w *http.ResponseWriter, errorCode int, msg string, customHeaders *map[string]string) {
   130  	var errorReply = ErrorReply{Code: errorCode, Message: msg}
   131  	errorReplyJSON, _ := json.Marshal(errorReply)
   132  
   133  	utils.HttpSendJSONResponse(w, errorStatus[errorCode], string(errorReplyJSON), customHeaders)
   134  }
   135  

View as plain text