status code versus error code

同事扔给我一张截图,显示errCode是5,说:

「已注册」的error code是5, errorCode 4开头的会到404页面, 5开头的会到505页面,

我们之前有通过的都是200

不不不

这个和 statusCode 是两码事儿
errCode 是业务错误代码
20x 40x 50x 是返回响应的状态码,通常是框架自动给的,若你的请求参数没有问题,系统通常都会返回状态码 200。但返回200并不意味着一切都对,在我们的项目中,状态码返回 200 并且 success 为 true 或者 errCode = 0才是真的没有问题

同事

哦,是这样,所以我们还是有statusCode

如果参数根本不符合约定,系统会立即拒绝你,那时候statusCode 会是 40x

如果参数都对,但后端的代码出了bug造成服务器不能正常返回,response会报 50x 表示前端没错,但服务器那边出问题了。
换言之如果是 20x,则你没错,我也没错,成功没有成功是业务逻辑的事情。
如果是 40x 意味着前端送过来的数据不符合后端要求,因此没有被进一步处理,而是被直接拒绝了
50x 表示前端送过来的东西没有大问题,但后端没有处理好出错了

同事

好的,那我明白了,我记错了,是statusCode才是指到505,404, 我以为都改成了errCode

所以 errCode 5 一定不能一杆子指到 505 页面去。

发现 50x 了去找后端工程师的麻烦一般不会错,如果 40x 了去找后端之前,最好先检查下自己的参数和出错信息

同事

status code 和 errCode 本质上是两个东西,errCode是返回数据的一部分

status code 是本次请求返回响应的状态码,可以理解为和返回数据是同一等级的

同事

那我们的error.response的数据结构有改变吗,我现在还是可以抓取到error.response.status,说明这部分应该是没有改的

这个是axios的约定,并不是后端能够决定的,我们结构的变化,只是response.data 结构的变化是 response.data 吧,我不是很确定,毕竟好久不写 axios 代码了

同事

是的,但是后来有加了一个data, 我们现在的结构变成这样的 [ Photo ]

对。后端只给了一个data键,图里的第一个data是 axios给的,需要注意的是,不同的框架给返回数据的键取的名字可能会不一样。如果不用axios而是用别的库,数据那一级的键的名字就很有可能不叫 data

同事

嗯嗯

话外音

我相信她这次真的搞明白了。哈。聊天记录稍加编辑就水了一篇博客,不过我相信它应该会对前端新人有所帮助。

Virtualbox MacOS VM setup / setup Rime/Squirrel input method (鼠须管) on macOS

  1. I successfully ran macOS High Sierra on my Ubuntu 18.04 laptop through VirtualBox. VirtualBox is great ! This article helps!
  2. Install Rime on macOS in five minutes
  3. download the zip file from latest release from the Tags page on GitHUB
  4. unzip the file and install the pkg file
  5. go to https://github.com/rime/plum to get the wubi package.
git clone https://github.com/rime/plum.git
cd plum
bash rime-install wubi pinyin-simp

The last step, is creating a default.custom.yaml file and putting it in ~/Library/Rime directory.

the content in my default.custom.yaml is

patch:
  schema_list:
    - schema: wubi_pinyin
    - schema: pinyin_simp

How make openvpn work with docker & avoid entering the password every time

I met this issue today morning and find the solution from the link I just refered.

I have made a few change to the solution to meet my case, this is why I wrote this article.

My version of fix-routes.sh

#!/bin/sh
echo "Adding custom route to $route_vpn_gateway with /24 mask..."
# ip route add 10.8.0.0/24 via $route_vpn_gateway
ip route add 192.168.178.0/24 via $route_vpn_gateway

echo "Removing /1 routes..."
ip route del 0.0.0.0/1 via $route_vpn_gateway
ip route del 128.0.0.0/1 via $route_vpn_gateway

I didn't use ip route add default via $route_vpn_gateway line because I didn't want to route everything through my VPN.

Changes to my openvpn profile

You can see I added an extra askpass line besides what the author has done. That line was added to help me get ride of the annoyance of entering the password for my private key every time I run the openvpn client program.

auth SHA256
auth-nocache
verb 3
askpass /home/davidwei/ovpn/pass.txt
script-security 2
route-up /etc/openvpn/fix-routes.sh

My ovpn start one-line script

davidwei@xps:~$ cat ~/bin/ovpn 
sudo openvpn ~/shukebeta.ovpn > /dev/null 2>&1 &

My pass.txt

davidwei@xps:~$ cat ~/ovpn/pass.txt 
my-very-secret-password

Notes for upgrading Debian 7 to Debian 10 on an old cheap VPS

前言

好几年前 123systems.net 打折的时候买了这个vps,年付只要 $3.8。不过配置还说得过去,256M 内存,10G ssd硬盘,每个月有512G流量,而且是 KVM的。那时候我还不知道啥是docker,买这台vps的目标很单纯,就是翻墙。然而当时 123systems.net 的服务非常烂,网络差,北京连过去经常丢包不说,而且还常down机,你提个ticket也要过上好几天才有人搭理。便宜没好货....看在乞丐价的份上,所以大爷我也忍了。不就3块8么,全当喂狗了。(所以后面不久我又从另一家买了一台配置高点的)。后来听说 123systems 被另一家 vps 提供商(对,就是chicago vps)收购了,我想那服务是不是会好点,就又连上去试了下。别说,网络真的稳定多了!也几乎不down机了,于是它就作为我的一个备用翻墙节点用起来了。这也才有了后面的年年续费。

在这台机器上我用了好几年 Ubuntu 14.04,但这台 vps 能够安装的操作系统模板,这么多年就一直没更新过。Ubuntu 最高是14.04, Centos是 6.5, Debian是7.3,Fedora 是 20。后来我玩 Docker 的时候想到这台机器是 kvm 啊,可以装docker的。于是就在上面装了Docker。后来随着 Docker 的一直升级,渐渐地新版 Docker 就不再支持 Ubuntu 14.04了。虽然用旧版也不是不行,但是我动了升级操作系统的念头。虽然模板中只有 14.04, 我总可以自己升级装好的系统嘛。然而升级并不顺利,我曾经磕磕绊绊升到 16.04 用了几天,发现不稳定(经常无故自动关机)就又回到了 14.04。后来经过一个不眠之夜,我装上了 Debian 7 又费了好大劲升级到 Debian 8 jessie。这个还比较稳定,就一直用着,直到昨天晚上。

昨天晚上闲逛的时候,我发现有一个人写了个一键自动重灌vps操作系统的脚本,支持 Debian 和 Centos, 支持指定版本号。 我的心又活络起来了。虽然那篇文间是2018年的,已经挺老了,但我还是打定主意打算试试,万一支持 Debian 10 呢。于是该备份的备份,然后开始尝试。不过作者说了不保证谁都能用,也不保证不丢数据。悲伤的是 他说对了

Debian装上启动不了,Centos 看文章下面的评论最高只能升级到 6.10 而我的目标是 7.7。罢了罢了!此路不通!看看天色已晚,已过夜里12点。我打算再最后试一次, 装个 Fedora 20 看看能不能一路升到最新。然而也不顺利。都1点了,去他的,睡觉了。

心里有事,睡得也不踏实,于是今天早上早早就起来了。不想再用Ubuntu了,于是重新装上 Deiban 7, 我想这次,就升级到 Deiban 8 和过去一样算了,回到原点凑付用吧。一台年付才3.8的破机器,不值当我花这么多时间折腾。然而 Debian 7 的源已经都死掉了,所以 Deiban 7 的系统也没法更新了,直接升 8 就好了。然而升 8 也不顺利,上次升级的时候并没有记笔记,所以好多问题东西要重新搜索,重新找(所以我今天在写笔记!)。就在到处找的过程中,我看到一个人在某个问题下评论说,他从 Debian 7 一直升级到了 Debian10,虽然花了很多时间,但感觉 amazing! 那我也试试唄。

现在是中午12点,开心的是我也搞定了那个升级,并且正在趁热乎把中间找资料的坎坷记录下来。那么口水到此结束,下面干货开始:

正文

  1. Debian 7 更新 /etc/apt/sources.list 为
deb http://deb.debian.org/debian/ jessie main contrib non-free
deb-src http://deb.debian.org/debian/ jessie main contrib non-free

deb http://security.debian.org/ jessie/updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free

deb http://deb.debian.org/debian/ jessie-updates main contrib non-free
deb-src http://deb.debian.org/debian/ jessie-updates main contrib non-free
  1. 用 root
apt-get -y update
apt-get -y install apt -t jessie
apt -y upgrade
apt-get -y dist-upgrade 
reboot

当然升级之路不会这么顺利,你肯定会遇到 no public key available for the following key IDs: xxxxx 和什么源已过期无法验证之类的报错。对于前者, 使用 apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1397BC53640DB551(请一定记着把key换成你那里看到的key);至于后者,可以用加参数大法apt-get -y -o Acquire::Check-Valid-Until=false update 解决。总起来说这一步还是蛮顺利的,升级完重新启动能直接进系统,网络什么都是好的。

  1. Jessie to Stretch,继续用root身份哦
sed -i 's/jessie/stretch/g' /etc/apt/sources.list
apt-get update
apt-get upgrade
apt-get dist-upgrade

应该说这个升级也蛮顺利的,除了升级完重启后....我就再也连接不上vps之外。还好有 Vnc 可以用,虽然网页上提供的 html5 vnc cient不能使,但我用本机的 vncviewer 能够连接上。 连上之后,发现除了网卡参数全丢了,其他都是正常的。那...网卡参数怎么设置?原来的参数都是啥?我不知道哇。于是又一通搜索,各种尝试,还是不行。最后想起 vps 控制台有 Reconfigure Networking 的功能,那就死马当活马医,试试吧。一试,好了!那么.....从7升到9已经是挺了不起的成就了,还升10么?升!

  1. Stretch to Buster,继续用 root 身份
sed -i 's/stretch/buster/g' /etc/apt/sources.list
apt-get update
apt-get upgrade
apt-get full-upgrade

这个升级也蛮顺利的,然而升级完重启后就连接不上vps的问题再一次发生了。根据刚才的经验用了 vps 控制台的 Reconfigure Networking 的功能,但这次没有那么走运,网还是不通。经过一番排查之后,发现这次系统注入参数时使用的网卡名字是 eth0,而我记得以前网卡的名字是 ens3。于是修改 /etc/network/interfaces 文件,把 eth0 改为 ens3 然后 systemctl start networking.service。成功!

每一次折腾,都能学到点什么,技术就是这么一点点折腾来的。但是如果不记录下来,下次再找又要花很多时间。所以...我又花了40分钟写这篇blog。不过还是很开心啊!把看似不可能事情最终搞成了,就是那样的开心!

参考资料: debian-wheezy-update-repository
参考资料: how-to-upgrade-debian-7-wheezy-to-10-buster-safely
参考资料: how-to-upgrade-debian-8-jessie-to-debian-9-stretch

Everything is hard before it is easy.

by Johann Wolfgang von Goethe