Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hokid committed Nov 14, 2020
0 parents commit f0a1acd
Show file tree
Hide file tree
Showing 17 changed files with 3,155 additions and 0 deletions.
203 changes: 203 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
out
gen
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

dist/
esnext/

1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version-git-message "chore: bump v%s"
152 changes: 152 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# format-axios-error

Pretty format [axios error](https://github.com/axios/axios#handling-errors). Build with [logform](https://github.com/winstonjs/logform) and can be used in browser and node js environments.

# Installation

Yarn

```bash
$ yarn add -E @redtea/format-axios-error
```

NPM

```bash
$ npm install -E @redtea/format-axios-error
```

# Usage

### NodeJS


```javascript
const axios = require('axios');
const {axiosError} = require('@redtea/format-axios-error');

const formatAxiosError = axiosError();

axios
.get('https://google.com/give-404')
.catch(error => {
console.log(
formatAxiosError
.transform(error)
)
});
```
Example above will print
```
{
config: {
url: 'https://google.com/give-404',
method: 'get',
headers: {
...[headers]
},
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1
},
isAxiosError: true,
stack: [stack trace],
response: {
status: 404,
data: [data],
headers: {
...[headers]
},
statusText: 'Not Found'
},
level: 'error',
message: 'Request failed with status code 404',
[Symbol(level)]: 'error',
[Symbol(message)]: 'Request failed with status code 404'
}
```
_Stack trace, response data and headers omitted in this example. To see full log message you can run code example locally_

### Winston

```javascript
const winston = require('winston');
const axios = require('axios');
const {axiosError} = require('@redtea/format-axios-error');
const {combine, json} = winston.format;

const logger = winston.createLogger({
level: 'info',
format: combine(
axiosError(),
json({ space: 2 })
),
transports: [
new winston.transports.Console(),
],
});

axios
.get('https://google.com/give-404')
.catch(error => logger.error(error))
```
Will print
```
{
"config": {
"url": "https://google.com/give-404",
"method": "get",
"headers": {
"Accept": "application/json, text/plain, */*",
"User-Agent": "axios/0.21.0"
},
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1
},
"isAxiosError": true,
"level": "error",
"stack": [stack],
"response": {
"status": 404,
"data": [data],
"headers": {
...[headers]
},
"statusText": "Not Found"
},
"message": "Request failed with status code 404"
}
```

### Browser

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>format-axios-error browser example</title>
<script src="https://unpkg.com/axios" type="application/javascript"></script>
<script src="https://unpkg.com/@redtea/format-axios-error" type="application/javascript"></script>
</head>
<body>
<script>
const formatAxiosError = AxiosErrorFormat.axiosError();
axios
.get('https://www.googleapis.com/give-404')
.catch(error => {
console.log(
formatAxiosError
.transform(error)
)
})
</script>
</body>
</html>
```
Loading

0 comments on commit f0a1acd

Please sign in to comment.