enhance: switch case for better case handling

This commit is contained in:
minoplhy 2024-04-14 21:45:37 +07:00
parent fa8788bd29
commit c4cc672c52
Signed by: minoplhy
GPG Key ID: 41D406044E2434BF

26
main.go
View File

@ -55,15 +55,15 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
// of the file input on the frontend // of the file input on the frontend
file, fileHeader, err := r.FormFile("file") file, fileHeader, err := r.FormFile("file")
if err != nil { if err != nil {
if err.Error() == "http: request body too large" { switch err.Error() {
case "http: request body too large":
http.Error(w, handler.ErrorResponseBuild(http.StatusRequestEntityTooLarge, "Request Body is too large!"), http.StatusRequestEntityTooLarge) http.Error(w, handler.ErrorResponseBuild(http.StatusRequestEntityTooLarge, "Request Body is too large!"), http.StatusRequestEntityTooLarge)
handler.ErrorLogBuilder([]string{r.RemoteAddr}, "Request Body is too large!") handler.ErrorLogBuilder([]string{r.RemoteAddr}, "Request Body is too large!")
return default:
} else {
http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError)
handler.ErrorLogBuilder([]string{r.RemoteAddr}, err.Error()) handler.ErrorLogBuilder([]string{r.RemoteAddr}, err.Error())
return
} }
return
} }
defer file.Close() defer file.Close()
@ -93,16 +93,22 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
// POST to chibisafe's /api/upload // POST to chibisafe's /api/upload
chibisafe_post, err := handler.UploadPost(Chibisafe_basepath, UploadHeaders, PostData) chibisafe_post, err := handler.UploadPost(Chibisafe_basepath, UploadHeaders, PostData)
if err != nil { if err != nil {
switch err.Error() {
default:
http.Error(w, handler.ErrorResponseBuild(http.StatusBadRequest, "Something went wrong!"), http.StatusBadRequest) http.Error(w, handler.ErrorResponseBuild(http.StatusBadRequest, "Something went wrong!"), http.StatusBadRequest)
handler.ErrorLogBuilder([]string{r.RemoteAddr, tempfilepath}, err.Error()) handler.ErrorLogBuilder([]string{r.RemoteAddr, tempfilepath}, err.Error())
}
return return
} }
var chibisafe_Response_Metadata handler.UploadResponseMeta var chibisafe_Response_Metadata handler.UploadResponseMeta
err = json.Unmarshal(chibisafe_post, &chibisafe_Response_Metadata) err = json.Unmarshal(chibisafe_post, &chibisafe_Response_Metadata)
if err != nil { if err != nil {
switch err.Error() {
default:
http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError)
handler.ErrorLogBuilder([]string{}, err.Error()) handler.ErrorLogBuilder([]string{}, err.Error())
}
return return
} }
handler.InfoLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, "Successfully obtained PUT keys") handler.InfoLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, "Successfully obtained PUT keys")
@ -110,8 +116,11 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
// PUT File to Network Storage // PUT File to Network Storage
_, err = handler.NetworkStoragePut(chibisafe_Response_Metadata.URL, PostData.ContentType, tempfilepath) _, err = handler.NetworkStoragePut(chibisafe_Response_Metadata.URL, PostData.ContentType, tempfilepath)
if err != nil { if err != nil {
switch err.Error() {
default:
http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError)
handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error())
}
return return
} }
handler.InfoLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, "Successfully PUT file to Network Storage") handler.InfoLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, "Successfully PUT file to Network Storage")
@ -140,16 +149,22 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
// POST to chibisafe's /api/upload/process // POST to chibisafe's /api/upload/process
PostProcess, err := handler.UploadProcessPost(Chibisafe_basepath, ProcessHeaders, PostProcessData) PostProcess, err := handler.UploadProcessPost(Chibisafe_basepath, ProcessHeaders, PostProcessData)
if err != nil { if err != nil {
switch err.Error() {
default:
http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError)
handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error())
}
return return
} }
var PostProcessResponse handler.UploadProcessResponseMeta var PostProcessResponse handler.UploadProcessResponseMeta
err = json.Unmarshal(PostProcess, &PostProcessResponse) err = json.Unmarshal(PostProcess, &PostProcessResponse)
if err != nil { if err != nil {
switch err.Error() {
default:
http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError)
handler.ErrorLogBuilder([]string{}, err.Error()) handler.ErrorLogBuilder([]string{}, err.Error())
}
return return
} }
handler.InfoLogBuilder([]string{r.RemoteAddr, PostProcessResponse.Name, tempfilepath}, fmt.Sprintf("Successfully Processed Response with UUID: %s", PostProcessResponse.UUID)) handler.InfoLogBuilder([]string{r.RemoteAddr, PostProcessResponse.Name, tempfilepath}, fmt.Sprintf("Successfully Processed Response with UUID: %s", PostProcessResponse.UUID))
@ -157,7 +172,10 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
// Delete Temporary file // Delete Temporary file
err = handler.DeleteFile(tempfilepath) err = handler.DeleteFile(tempfilepath)
if err != nil { if err != nil {
switch err.Error() {
default:
handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error())
}
return return
} }
handler.InfoLogBuilder([]string{r.RemoteAddr, tempfilepath}, "Successfully Deleted Temporary file from local disk") handler.InfoLogBuilder([]string{r.RemoteAddr, tempfilepath}, "Successfully Deleted Temporary file from local disk")