Skip to content

add types and introduce writeErrorToResponse - #77

Merged
shimks merged 1 commit into
masterfrom
export-build-response-data
Aug 30, 2018
Merged

add types and introduce writeErrorToResponse#77
shimks merged 1 commit into
masterfrom
export-build-response-data

Conversation

@shimks

@shimksshimks commented Aug 8, 2018

Copy link
Copy Markdown
Contributor

Description

related to loopbackio/loopback-next#1598

Related issues

  • connect to <link_to_referenced_issue>

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

@shimks
shimksforce-pushed the export-build-response-data branch from a00e706 to 237bb8aCompareAugust 8, 2018 17:37
@shimks

Copy link
Copy Markdown
ContributorAuthor

Downstream failure has to do with linting afaik

Comment threadlib/handler.js Outdated
};
};

module.exports.buildResponseData = buildResponseData;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be exported from /lib/data-builder.js and not this file which is just importing in the data-builder.

@bajtos

Copy link
Copy Markdown
Member

See my loopbackio/loopback-next#1598 (comment). I think we need to expose a slightly larger functionality.

It would be also great to add a .d.ts file describing the public API of strong-error-handler, so that we can consume this module in LB4 in a type-safe way.

@shimksshimks left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remind me to add in documentation please

Comment threadtypes/handler.d.ts Outdated
err: Error,
req: Express.Request,
res: Express.Response
) => void | Socket;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the handler function is accepting four arguments including a callback, this is an Express requirement. Also the handler should not be returning Socket. If it does, then it's a bug in the implementation. Conceptually, the handler returns void and may invoke next.

exporttypestrongErrorHandler=(err: Error,req: Express.Request,res: Express.Response,next: (err?: any)=>void,)=>void;

Comment threadindex.d.ts Outdated
@@ -0,0 +1,14 @@
import * as Handler from './types/handler';
import * as Options from './types/options';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, having multiple .d.ts files feel like overkill to me, considering that our typings are less than 30 lines of code in total. Not a big deal though, if you prefer this way then I am ok with that.

Comment threadindex.d.ts Outdated

export = errorHandler;

declare function errorHandler(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this errorHandlerFactory because it is a factory function returning back a middleware handler.

Comment threadindex.d.ts Outdated
options?: Options.ErrorHandlerOptions
): errorHandler.StrongErrorHandler

declare namespace errorHandler {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this namespace? Isn't it enough to export Handler under the name ErrorHandler?

import{HandlerasErrorHandler}from'./types/handler';import{ErrorHandlerOptions}from'./types/options';export{ErrorHandler,ErrorHandlerOptions};

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code would have worked if exported functions were properties of module.exports. Instead, our module exports createStrongErrorHandler as module.exports itself, so the export above won't work.

I don't understand everything here (like the export = errorhandler syntax) but I've managed to get it to work as expected following this template: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-function-d-ts.html

@shimks
shimksforce-pushed the export-build-response-data branch 3 times, most recently from 5bf7a10 to 6775c04CompareAugust 14, 2018 18:02
@shimksshimks changed the title export buildResponseDataadd types and introduce writeErrorToResponseAug 14, 2018
@shimks
shimksforce-pushed the export-build-response-data branch from a84afee to 4007a1eCompareAugust 14, 2018 21:06

@bajtosbajtos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Comment threadREADME.md Outdated
http
.createServer((req, res) => {
if (errShouldBeThrown) {
writeErrorToResponse(new Error('something went wrong'), req, res);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please show how to pass options too (via the fourth argument).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please show how to pass options too (via the fourth argument).

☝️

@bajtos

Copy link
Copy Markdown
Member

Please check why a downstream build of strong-remoting@master is failing (details). We don't want to introduce a regression.

Comment threadindex.d.ts Outdated
*/
interface ErrorHandlerOptions {
debug?: boolean;
log?: boolean;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we split this interface into two smaller interfaces, one containing the options used by both the middleware and the writer, the other adding options specific to the middleware (i.e. log)?

interfaceErrorHandlerCoreOptions{debug?: boolean;// etc.}interfaceErrorHandlerMiddlewareOptionsextendsErrorHandlerCoreOptions{log?: boolean;}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they passed in as two separate objects? Since both are optional properties I think a single *Options object is better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the log property is not supported by writeErrorToResponse. Ideally, the interface used by writeErrorToResponse options should not offer properties that are not supported by the function.

declarefunctionerrorHandlerFactory(options?: errorHandlerFactory.ErrorHandlerMiddlewareOptions
: errorHandlerFactory.StrongErrorHandler;functionwriteErrorToResponse(err: Error,req: Express.Request,res: Express.Response,options?: errorHandlerFactory.ErrorHandlerCoreOptions): void;

On the second thought, I think the following interface names may be better because they are shorter:

  • ErrorHandlerOptions (used by middleware factory)
  • ErrorWriterOptions (used by writeErrorToResponse)

Anyhow, this is not a big deal, I am ok with the current version where there is a single interface only.

@virkt25

Copy link
Copy Markdown

strong-remoting is failing due to linting errors (all the tests pass). I've opened a PR to fix the linting issues strongloop/strong-remoting#452

@shimks

Copy link
Copy Markdown
ContributorAuthor

@slnode test please

@b-admikeb-admike left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jannyHoujannyHou left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM and the same suggestion as Miroslav post in the README.md file.

@bajtosbajtos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have two minor comments to consider.

No further review is necessary as far as I am concerned.

Comment threadREADME.md Outdated
http
.createServer((req, res) => {
if (errShouldBeThrown) {
writeErrorToResponse(new Error('something went wrong'), req, res);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please show how to pass options too (via the fourth argument).

☝️

Comment threadindex.d.ts Outdated
/**
* Error-handling middleware function. Includes server-side logging
*/
type StrongErrorHandler = (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move types to the top level please, so that users don't have to prefix their usage with errorHandlerFactory namespace?

I am referring to the following types:

  • StrongErrorHandler
  • ErrorWriterOptions
  • ErrorHandlerOptions

Of course, writeErrorToResponse must stay inside errorHandlerFactory namespace, because that's how the JavaScript code is structured.

declareinterfaceErrorWriterOptions{// ...}declareinterfaceErrorHandlerOptions{// ...}declaretypeStrongErrorHandler=// ...declarefunctionerrorHandlerFactory// ...declarenamespaceerrorHandlerFactory{functionwriteErrorToResponse// ...}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, moving the types outside of the namespace does not allow us to export the types since module.exports is only the handler function itself.

As long as the error handler function isn't used from the module, the users shoudn't have to use the namespace if that was what you were concerned about. See here https://github.com/strongloop/loopback-next/blob/4e180b286bf6a353067995795a7770522332588a/packages/rest/src/providers/reject.provider.ts#L10

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, moving the types outside of the namespace does not allow us to export the types since module.exports is only the handler function itself.

I see what you mean now! I was not aware of this limitation, thank you for explaining it to me.

@shimks
shimksforce-pushed the export-build-response-data branch from cad039c to 80ebf30CompareAugust 30, 2018 16:09
@shimks
shimks merged commit 4e29906 into masterAug 30, 2018
@shimks
shimks deleted the export-build-response-data branch August 30, 2018 16:53
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@shimks@bajtos@virkt25@jannyHou@b-admike