Giter Club home page Giter Club logo

json-resp's People

Contributors

bauerm97 avatar dependabot-preview[bot] avatar emmeff avatar tfrisch06 avatar tri-adam avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-resp's Issues

Remove JSONErrorUnauthorized?

I think it might be wise to remove JSONErrorUnauthorized. It's not obvious (to me anyways) how one would use it directly with any of the rest of the json-resp API.

The jsonresp.Error type is intended to allow a status code and optional message to be included in a JSON-encoded response. The idea is that the entity sending the JSON-encoded response calls WriteError() with the status code and message. The recipient, upon receiving a non-successful HTTP status code, can use ReadError() to retrieve a jsonresp.Error that includes the status code and message.

JSONErrorUnauthorized isn't really useful to pass to WriteError(), since it takes an error string and code int rather than a jsonresp.Error. You can't compare it to any error you receive from ReadError() easily. #30 would open this up as a possibility, but in practice I'm not sure errors.Is(err, JSONErrorUnauthorized) is as useful as doing errors.Is(err, &Error{Code: http.StatusUnauthorized}). The former will only match errors where the Message is exactly "Unauthorized", whereas the latter would match any unauthorized response.

@EmmEff, would love your input here. I know there's some use of this in the Sylabs code base, and I guess I'm curious whether JSONErrorUnauthorized can/should be moved somewhere internal where it's used?

Verify Go 1.13 Compatibility

Update CI to verify this module is (and continues to be) compatible with Go v1.13. The cache_go_mod step should be unnecessary, with the default module proxy serving a similar purpose.

Add Func to Read Error

This package gives the ability to provide detailed error messages, but in practice this is somewhat awkward to use with the current API. Consider the case where a caller gets a non-OK status code back, but is unsure whether a detailed error is present. In this case, the caller must differentiate a failure of jsonresp.ReadResponse to unmarshal versus a detailed error itself. In the case of a failure to unmarshal, it is often desirable to report other more relevant information such as the HTTP status code. This can currently be accomplished with logic such as:

	if res.StatusCode != http.StatusOK {
		if err := jsonresp.ReadResponse(res.Body, nil); err != nil {
			if err, ok := err.(*jsonresp.Error); ok {
				return err
			}
		}
		return jsonresp.NewError(res.StatusCode, "")
	}

This is not very straight forward. To help with clarity, I propose introducing a func ReadError(r io.Reader) error function, that will return the error detail when one is possible in r, and nil otherwise. Then, the logic is more readable:

	if res.StatusCode != http.StatusOK {
		if err := jsonresp.ReadError(res.Body); err != nil {
			return err
		}
		return jsonresp.NewError(res.StatusCode, "")
	}

CI Workflow Improvements

There are some recommendations about how best to pass around source code and dependencies on the CircleCI blog. Use checkout to retrieve source, and restore_cache to fetch cached Go modules.

Remove Dep

As @mikegray points out, json-resp has no external dependencies, so it is not necessary to use dep for this package.

Remove dep, and all references to it in the README, CircleCI config, and .gitignore file.

Subtle Bug in SafeWriteResponse

I'm a little concerned that in practice, usage of SafeWriteResponse() always results in a subtle bug.

As I understand it, SafeWriteResponse() is intended to address the following scenario:

  1. A user calls WriteResponse(), which calls WriteResponsePage(), which fails to encode JSON.
  2. The caller receives an error.
  3. The caller now wishes http.StatusInternalServerError to be set in the HTTP response.

SafeWriteResponse combines these three steps.

This leads me to where I think there is an issue. According to the http.ResponseWriter docs, the response code is written to the caller on the first call to (http.ResponseWriter).WriteError() or (http.ResponseWriter).Write() (whichever comes first.) In the above example, this always occurs in step 1, and thus any future call to set an HTTP response code is ignored.

I do think there is merit to the above behaviour. We could make this the default in WriteResponse() if we encoded the JSON to a buffer to ensure it could be encoded before writing it to the response. Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.