1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-04 21:15:24 +02:00

wrap errors up, log at top level only

This commit is contained in:
Harvey Kandola 2017-08-03 10:00:24 +01:00
parent ecc94f31c9
commit 476403bf46
28 changed files with 899 additions and 804 deletions

View file

@ -17,7 +17,6 @@ import (
"strings"
api "github.com/documize/community/core/convapi"
"github.com/documize/community/core/log"
"github.com/documize/community/core/stringutil"
"golang.org/x/net/context"
"golang.org/x/net/html"
@ -192,7 +191,6 @@ func (h *htmlToSplit) renderAppend(c *html.Node) error {
ebyt := stringutil.EscapeHTMLcomplexCharsByte(byt)
if len(ebyt) > maxBody {
msg := fmt.Sprintf("(Documize warning: HTML render element ignored, size of %d exceeded maxBody of %d.)", len(ebyt), maxBody)
log.Info(msg)
ebyt = []byte("<p><b>" + msg + "</b></p>")
}
if len(h.thisSect.Body)+len(ebyt) > maxBody {

View file

@ -22,7 +22,6 @@ import (
"github.com/documize/community/core/api/convert/html"
"github.com/documize/community/core/api/convert/md"
api "github.com/documize/community/core/convapi"
"github.com/documize/community/core/log"
"github.com/documize/community/domain"
"github.com/documize/glick"
)
@ -34,14 +33,14 @@ var insecure = "false"
type infoLog struct{}
func (i infoLog) Write(b []byte) (int, error) {
log.Info(string(b))
// log.Info(string(b))
return len(b), nil
}
type errorLog struct{}
func (i errorLog) Write(b []byte) (int, error) {
log.ErrorString(string(b))
// log.ErrorString(string(b))
return len(b), nil
}
@ -107,7 +106,6 @@ func Setup(s *domain.Store) error {
} else {
json, err = ioutil.ReadFile(PluginFile)
if err != nil {
log.Info("Plugin file '" + PluginFile + "' not found, using no plugins")
json = []byte(" [ ] \n")
err = nil
}

View file

@ -13,7 +13,6 @@ package convapi
import (
"encoding/json"
"github.com/documize/community/core/log"
"net/http"
)
@ -33,7 +32,6 @@ func SetJSONResponse(w http.ResponseWriter) {
// WriteError to the http.ResponseWriter, taking care to provide the correct
// response error code within the JSON response.
func WriteError(w http.ResponseWriter, err error) {
response := apiJSONResponse{}
response.Message = err.Error()
response.Success = false
@ -57,20 +55,13 @@ func WriteError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
}
json, err := json.Marshal(response)
if err != nil {
log.Error("json.Marshal", err)
}
if _, err := w.Write(json); err != nil {
log.Error("write to ResponseWriter", err)
}
json, _ := json.Marshal(response)
w.Write(json)
}
// WriteErrorBadRequest provides feedback to a Documize client on an error,
// where that error is described in a string.
func WriteErrorBadRequest(w http.ResponseWriter, message string) {
response := apiJSONResponse{}
response.Message = message
response.Success = false
@ -79,12 +70,7 @@ func WriteErrorBadRequest(w http.ResponseWriter, message string) {
response.Code = 400
w.WriteHeader(http.StatusBadRequest)
json, err := json.Marshal(response)
if err != nil {
log.Error("json.Marshal", err)
}
json, _ := json.Marshal(response)
if _, err := w.Write(json); err != nil {
log.Error("write to ResponseWriter", err)
}
w.Write(json)
}

View file

@ -1,70 +0,0 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
// Package log provides centralized logging for the Documize application.
package log
import (
"bytes"
"fmt"
"os"
"runtime"
log "github.com/Sirupsen/logrus"
)
var environment = "Non-production"
func init() {
log.SetFormatter(new(log.TextFormatter))
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}
// Debug logs a message for debug purposes.
func Debug(message string) {
log.WithFields(log.Fields{"env": environment}).Debug(message)
}
// Info logs a message for information purposes.
func Info(message string) {
log.WithFields(log.Fields{"env": environment}).Info(message)
}
// TestIfErr is used by the test code to signal that a test being run should error, it is reset if an error occurs.
var TestIfErr bool
// ErrorString logs an error, where there is not an error value.
func ErrorString(message string) {
TestIfErr = false
log.WithFields(log.Fields{"env": environment}).Error(message)
}
// Error logs an error, if non-nil, with a message to give some context.
func Error(message string, err error) {
if err != nil {
TestIfErr = false
stack := make([]byte, 4096)
runtime.Stack(stack, false)
if idx := bytes.IndexByte(stack, 0); idx > 0 && idx < len(stack) {
stack = stack[:idx] // remove trailing nulls from stack dump
}
log.WithFields(log.Fields{"env": environment, "error": err.Error(), "stack": fmt.Sprintf("%s", stack)}).Error(message)
//log.WithField("error: "+message, err.Error()).Errorf("%q\n%s\n", err, stack[:])
}
}
// IfErr logs an error if one exists.
// It is a convenience wrapper for Error(), with no context message.
func IfErr(err error) {
Error("", err)
}

View file

@ -15,7 +15,6 @@ import (
"crypto/rand"
"encoding/hex"
"github.com/documize/community/core/log"
"golang.org/x/crypto/bcrypt"
)
@ -32,8 +31,8 @@ func GenerateSalt() string {
// GenerateRandom returns a string of the specified length using crypo/rand
func GenerateRandom(size int) string {
b := make([]byte, size)
_, err := rand.Read(b)
log.IfErr(err)
rand.Read(b)
return hex.EncodeToString(b)
}
@ -42,11 +41,7 @@ func GeneratePassword(password string, salt string) string {
pwd := []byte(salt + password)
// Hashing the password with the cost of 10
hashedPassword, err := bcrypt.GenerateFromPassword(pwd, 10)
if err != nil {
log.Error("GeneratePassword failed", err)
}
hashedPassword, _ := bcrypt.GenerateFromPassword(pwd, 10)
return string(hashedPassword)
}