-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show Micro.blog error message when available
- Loading branch information
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Factory responsible for creating Errors from network responses. | ||
*/ | ||
export class ErrorFactory { | ||
|
||
// The Micropub specification defines response errors as: | ||
// | ||
// { | ||
// "error": "invalid_request", | ||
// "error_description": "The post with the requested URL was not found" | ||
// } | ||
// | ||
// This method is responsible for building an Error instance with the | ||
// information contained in the response error (if any). | ||
public static async makeErrorFromResponse( | ||
response: Response | ||
): Promise<Error> { | ||
let errorDetails = '' | ||
|
||
const errorResponse = await response.json() | ||
const hasError = errorResponse.error | ||
const hasDescription = errorResponse.error_description | ||
|
||
if (hasError && hasDescription) { | ||
errorDetails = errorResponse.error_description | ||
} else if (hasError) { | ||
errorDetails = 'Micro.blog error code: ' + errorResponse.error | ||
} else { | ||
errorDetails = 'Network error: ' + response.status | ||
} | ||
|
||
return new Error(errorDetails) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters