github.com/dgraph-io/dgraph@v1.2.8/graphql/api/panics.go (about) 1 /* 2 * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package api 18 19 import ( 20 "runtime/debug" 21 22 "github.com/golang/glog" 23 "github.com/pkg/errors" 24 ) 25 26 // PanicHandler catches panics to make sure that we recover from panics during 27 // GraphQL request execution and return an appropriate error. 28 // 29 // If PanicHandler recovers from a panic, it logs a stack trace, creates an error 30 // and applies fn to the error. 31 func PanicHandler(requestID string, fn func(error)) { 32 if err := recover(); err != nil { 33 glog.Errorf("[%s] panic: %s.\n trace: %s", requestID, err, string(debug.Stack())) 34 35 fn(errors.Errorf("[%s] Internal Server Error - a panic was trapped. "+ 36 "This indicates a bug in the GraphQL server. A stack trace was logged. "+ 37 "Please let us know : https://github.com/dgraph-io/dgraph/issues.", 38 requestID)) 39 } 40 }