当前位置: 首页>>代码示例>>Golang>>正文


Golang errors.New函数代码示例

本文整理汇总了Golang中github.com/wendal/errors.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: BuildCommentsWidget

func BuildCommentsWidget(cnf Mapper, topCtx mustache.Context) (Widget, error) {
	log.Println("Comments >>", cnf.Layout())
	switch cnf.Layout() {
	case "disqus":
		disqus := cnf[cnf.Layout()].(map[string]interface{})
		short_name := disqus["short_name"]
		if short_name == nil {
			return nil, errors.New("CommentsWidget Of disqus need short_name")
		}
		self := make(CommentsWidget)
		self["comments"] = fmt.Sprintf(Comments_disqus, short_name)
		return self, nil
	case "uyan":
		uyan := cnf[cnf.Layout()].(map[string]interface{})
		uid := uyan["uid"]
		self := make(CommentsWidget)
		self["comments"] = fmt.Sprintf(tpl_uyan, uid)
		return self, nil
	case "duoshuo":
		duoshuo := cnf[cnf.Layout()].(map[string]interface{})
		short_name := duoshuo["short_name"]
		if short_name == nil {
			return nil, errors.New("CommentsWidget Of duoshuo need short_name")
		}
		self := make(CommentsWidget)
		self["comments"] = fmt.Sprintf(Comments_duoshuo, short_name)
		return self, nil
	}
	// 其他的,想不到还有啥,哈哈,需要其他的就报个issue吧
	return nil, errors.New("CommentsWidget Only for disqus yet")
}
开发者ID:ywjno,项目名称:gor,代码行数:31,代码来源:widgets.go

示例2: BuildAnalyticsWidget

func BuildAnalyticsWidget(cnf Mapper, topCtx mustache.Context) (Widget, error) {
	switch cnf.Layout() {
	case "google": // 鼎鼎大名的免费,但有点拖慢加载速度,原因你懂的
		google := cnf[cnf.Layout()].(map[string]interface{})
		tracking_id := google["tracking_id"]
		if tracking_id == nil {
			return nil, errors.New("AnalyticsWidget Of Google need tracking_id")
		}
		self := make(AnalyticsWidget)
		self["analytics"] = fmt.Sprintf(Analytics_google, tracking_id)
		return self, nil
	case "cnzz": //免费,而且很快,但强制嵌入一个反向链接,靠!
		cnzz := cnf[cnf.Layout()].(map[string]interface{})
		tracking_id := cnzz["tracking_id"]
		if tracking_id == nil {
			return nil, errors.New("AnalyticsWidget Of CNZZ need tracking_id")
		}
		self := make(AnalyticsWidget)
		self["analytics"] = fmt.Sprintf(tpl_cnzz, tracking_id, tracking_id)
		return self, nil
	}
	// 其他的尚不支持, 如果需要,请报个issue吧
	return nil, errors.New("AnalyticsWidget Only for Goolge/CNZZ yet")

}
开发者ID:ywjno,项目名称:gor,代码行数:25,代码来源:widgets.go

示例3: BuildCustomWidget

func BuildCustomWidget(name string, dir string, cnf Mapper) (Widget, []string, error) {
	layoutName, ok := cnf["layout"]
	if !ok || layoutName == "" {
		log.Println("Skip Widget : " + dir)
		return nil, nil, nil
	}

	layoutFilePath := dir + "/layouts/" + layoutName.(string) + ".html"
	f, err := os.Open(layoutFilePath)
	if err != nil {
		return nil, nil, errors.New("Fail to load Widget Layout" + dir + "\n" + err.Error())
	}
	defer f.Close()
	cont, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, nil, errors.New("Fail to load Widget Layout" + dir + "\n" + err.Error())
	}

	assets := []string{}
	for _, js := range cnf.GetStrings("javascripts") {
		path := "/assets/" + dir + "/javascripts/" + js
		assets = append(assets, fmt.Sprintf("<script type=\"text/javascript\" src=\"%s\"></script>", path))
	}
	for _, css := range cnf.GetStrings("stylesheets") {
		path2 := "/assets/" + dir + "/stylesheets/" + css
		assets = append(assets, fmt.Sprintf("<link href=\"%s\" type=\"text/css\" rel=\"stylesheet\" media=\"all\">", path2))
	}

	return &CustomWidget{name, &DocContent{string(cont), string(cont), nil}, cnf}, assets, nil

}
开发者ID:ywjno,项目名称:gor,代码行数:31,代码来源:widgets.go

示例4: parseLanguageAbility

func parseLanguageAbility(req *http.Request) ([]db.LanguageAbility, error) {
	var languages []db.LanguageAbility

	rawJson := req.FormValue("langAbilities")
	if len(rawJson) == 0 {
		return languages, errors.New("No argument")
	}

	decoder := json.NewDecoder(strings.NewReader(rawJson))
	if _, e := decoder.Token(); e != nil { //The first array bracket
		return languages, errors.New("Wrong json format")
	}

	element := RawLang{}
	for decoder.More() {
		if e := decoder.Decode(&element); e != nil {
			continue
		}

		if len(element.Abilities) < 4 {
			continue
		}
		lang := db.LanguageAbility{
			Name:      element.LangName,
			Listening: uint(element.Abilities[0].Value),
			Speaking:  uint(element.Abilities[1].Value),
			Reading:   uint(element.Abilities[2].Value),
			Writing:   uint(element.Abilities[3].Value),
		}
		languages = append(languages, lang)
	}

	decoder.Token() //The last array bracket
	return languages, nil
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:35,代码来源:form.go

示例5: GetSessionUserPermission

func GetSessionUserPermission(req *http.Request) (UserPermission, error) {
	if v, err := GetUserSessionValue(req, USER_PERMISSION_SESSION_KEY); err != nil || v == nil {
		return UserPermission(0), errors.New("Invalid session key")
	} else {
		if perm, found := v.(UserPermission); found {
			return perm, nil
		} else {
			return UserPermission(0), errors.New("Invalid session permission format")
		}
	}
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:11,代码来源:routines.go

示例6: BuildCommentsWidget

func BuildCommentsWidget(cnf Mapper, topCtx mustache.Context) (Widget, error) {
	if cnf.Layout() != "disqus" {
		return nil, errors.New("CommentsWidget Only for disqus yet")
	}
	disqus := cnf[cnf.Layout()].(map[string]interface{})
	short_name := disqus["short_name"]
	if short_name == nil {
		return nil, errors.New("CommentsWidget Of disqus need short_name")
	}
	self := make(CommentsWidget)
	self["comments"] = fmt.Sprintf(Comments_disqus, short_name)
	return self, nil
}
开发者ID:RaymondChou,项目名称:gor,代码行数:13,代码来源:widgets.go

示例7: BuildAnalyticsWidget

func BuildAnalyticsWidget(cnf Mapper, topCtx mustache.Context) (Widget, error) {
	if cnf.Layout() != "google" {
		return nil, errors.New("AnalyticsWidget Only for Goolge yet")
	}
	google := cnf[cnf.Layout()].(map[string]interface{})
	tracking_id := google["tracking_id"]
	if tracking_id == nil {
		return nil, errors.New("AnalyticsWidget Of Google need tracking_id")
	}
	self := make(AnalyticsWidget)
	self["analytics"] = fmt.Sprintf(Analytics_google, tracking_id)
	return self, nil
}
开发者ID:RaymondChou,项目名称:gor,代码行数:13,代码来源:widgets.go

示例8: GetSessionGMId

func GetSessionGMId(req *http.Request) (bson.ObjectId, error) {
	if v, err := GetGMSessionValue(req, GM_ID_SESSION_KEY); err != nil || v == nil {
		return bson.ObjectId(""), errors.New("Invalid session id format")
	} else {
		if str, found := v.(string); found {
			if bson.IsObjectIdHex(str) {
				return bson.ObjectIdHex(str), nil
			} else {
				return bson.ObjectId(""), errors.New("Invalid session id format")
			}
		} else {
			return bson.ObjectId(""), errors.New("Invalid session id format")
		}
	}
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:15,代码来源:routines.go

示例9: parseTopic

func parseTopic(req *http.Request) (uint, error) {
	for i, v := range TOPICS {
		if len(req.FormValue(v)) == 0 {
			continue
		}
		return uint(i), nil
	}
	return 1, errors.New("Not Found")
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:9,代码来源:form.go

示例10: parseSchoolGrade

func parseSchoolGrade(req *http.Request) (string, error) {
	gradeType := req.FormValue("gradeType")
	schoolGrade := req.FormValue("schoolGrade")

	var numGrade int
	if n, _ := fmt.Sscanf(schoolGrade, "%d", &numGrade); n < 1 || numGrade < 0 {
		return "", errors.New("Invalid Grade")
	}

	return fmt.Sprintf("%[email protected]%d", gradeType, numGrade), nil
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:11,代码来源:form.go

示例11: SetGMSessionValue

func SetGMSessionValue(req *http.Request, resp http.ResponseWriter, key, value interface{}) error {
	//Ignore the error since sometimes the browser side coolie storage is broken
	//But we still can assign new cookies
	s, _ := SessionStorage.Get(req, GM_AUTH_SESSION)
	if s == nil {
		return errors.New("Session " + GM_AUTH_SESSION + " not available")
	}

	s.Values[key] = value
	return s.Save(req, resp)
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:11,代码来源:routines.go

示例12: initStorage

func initStorage() {
	if !Config.IsSet("storage.serviceAccountEmail") || !Config.IsSet("storage.privateKeyPath") {
		panic(errors.New("storage.serviceAccountEmail or storage.privateKeyPath not set"))
	}

	StorageServiceAccountEmail = Config.GetString("storage.serviceAccountEmail")
	//LogD.Println("Service account: " + StorageServiceAccountEmail)

	privateKeyPath := Config.GetString("storage.privateKeyPath")
	if file, err := os.Open(privateKeyPath); err != nil {
		panic(errors.New("storage.privateKeyPath not exist"))
	} else {
		defer file.Close()
		if StoragePrivateKey, err = ioutil.ReadAll(file); err != nil {
			panic(errors.New("storage.privateKeyPath read file error"))
		} else {
			//LogD.Printf("Private key length: %d\n", len(StoragePrivateKey))
		}
	}
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:20,代码来源:public.go

示例13: LoadWidgets

// 遍历目录,加载挂件
func LoadWidgets(topCtx mustache.Context) ([]Widget, string, error) {
	widgets := make([]Widget, 0)
	assets := ""

	err := filepath.Walk("widgets", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return nil
		}
		if !info.IsDir() {
			return nil
		}
		cnf_path := path + "/config.yml"
		fst, err := os.Stat(cnf_path)
		if err != nil || fst.IsDir() {
			return nil //ignore
		}
		cnf, err := ReadYml(cnf_path)
		if err != nil {
			return errors.New(cnf_path + ":" + err.Error())
		}
		if cnf["layout"] != nil {
			widget_enable, ok := cnf["layout"].(bool)
			if ok && !widget_enable {
				log.Println("Disable >", cnf_path)
			}
		}
		builderFunc := WidgetBuilders[info.Name()]
		if builderFunc == nil { // 看看是否符合自定义挂件的格式
			_widget, _assets, _err := BuildCustomWidget(info.Name(), path, cnf)
			if _err != nil {
				log.Println("NO WidgetBuilder >>", cnf_path, _err)
			}
			if _widget != nil {
				widgets = append(widgets, _widget)
				if _assets != nil {
					for _, asset := range _assets {
						assets += asset + "\n"
					}
				}
			}
			return nil
		}
		widget, err := builderFunc(cnf, topCtx)
		if err != nil {
			return err
		}
		widgets = append(widgets, widget)
		log.Println("Load widget from ", cnf_path)
		return nil
	})
	return widgets, assets, err
}
开发者ID:ywjno,项目名称:gor,代码行数:53,代码来源:widgets.go

示例14: GetSessionUserId

func GetSessionUserId(req *http.Request) (bson.ObjectId, error) {

	if data := req.Header.Get(GM_PERMITTED_HEADER_KEY); len(data) != 0 && bson.IsObjectIdHex(data) {
		//Controlled user id
		user_id := bson.ObjectIdHex(data)
		return user_id, nil
	}

	if v, err := GetUserSessionValue(req, USER_ID_SESSION_KEY); err != nil || v == nil {
		return bson.ObjectId(""), errors.New("Invalid session id format")
	} else {
		if str, found := v.(string); found {
			if bson.IsObjectIdHex(str) {
				return bson.ObjectIdHex(str), nil
			} else {
				return bson.ObjectId(""), errors.New("Invalid session id format")
			}
		} else {
			return bson.ObjectId(""), errors.New("Invalid session id format")
		}
	}
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:22,代码来源:routines.go

示例15: parseRecommendationLetters

func parseRecommendationLetters(req *http.Request) ([]public.BasicUser, error) {
	var letters []public.BasicUser

	rawJson := req.FormValue("recommendationLetters")
	if len(rawJson) == 0 {
		return letters, errors.New("No argument")
	}

	decoder := json.NewDecoder(strings.NewReader(rawJson))
	if _, e := decoder.Token(); e != nil { //The first array bracket
		return letters, errors.New("Wrong json format")
	}

	element := public.BasicUser{}
	for decoder.More() {
		if e := decoder.Decode(&element); e != nil {
			continue
		}
		letters = append(letters, element)
	}

	decoder.Token() //The last array bracket
	return letters, nil
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:24,代码来源:form.go


注:本文中的github.com/wendal/errors.New函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。